<commit_before>package main

import (
	"fmt"

	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/s3"
	"github.com/go-ini/ini"
	"net/url"
)

type Site struct {
	Domain   string
	AuthUser string
	AuthPass string

	BucketRegion string
	BucketName   string

	UseImgix bool
	BaseUrl  string

	AWS_SECRET_KEY_ID string
	AWS_SECRET_KEY    string

	MetaTitle  string
	SiteTitle  string
	AlbumTitle string
}

func LoadSiteFromFile(path string) (*Site, error) {
	cfg, err := ini.Load(path)
	if err != nil {
		return nil, err
	}

	section, err := cfg.GetSection("")
	if err != nil {
		return nil, err
	}

	requiredFields := []string{
		"Domain", "Region", "Bucket", "UseImgix", "BaseUrl", "AWSKeyId", "AWSKey", "MetaTitle",
		"SiteTitle", "AlbumTitle",
	}
	for _, v := range requiredFields {
		if !section.HasKey(v) {
			return nil, fmt.Errorf("Config file %s does not contain value of required key %s", path, v)
		}
	}

	bucketName := section.Key("Bucket").String()
	s := &Site{
		Domain: section.Key("Domain").String(),

		BucketRegion: section.Key("Region").String(),
		BucketName:   bucketName,

		UseImgix: section.Key("UseImgix").String() == "1",
		BaseUrl:  section.Key("BaseUrl").String(),

		AWS_SECRET_KEY_ID: section.Key("AWSKeyId").String(),
		AWS_SECRET_KEY:    section.Key("AWSKey").String(),

		MetaTitle:  section.Key("MetaTitle").String(),
		SiteTitle:  section.Key("SiteTitle").String(),
		AlbumTitle: section.Key("AlbumTitle").String(),
	}

	if section.HasKey("AuthUser") && section.HasKey("AuthPass") {
		s.AuthUser = section.Key("AuthUser").String()
		s.AuthPass = section.Key("AuthPass").String()
	}

	return s, nil
}

func (s *Site) GetS3Service() (*s3.S3, error) {
	sess, err := session.NewSession(&aws.Config{
		Region:      aws.String(s.BucketRegion),
		Credentials: credentials.NewStaticCredentials(s.AWS_SECRET_KEY_ID, s.AWS_SECRET_KEY, ""),
	})

	if err != nil {
		return nil, err
	}

	return s3.New(sess), nil
}

func (s *Site) GetAllObjects() ([]*s3.Object, error) {
	svc, err := s.GetS3Service()
	if err != nil {
		return nil, err
	}

	objects, err := svc.ListObjects(&s3.ListObjectsInput{Bucket: aws.String(s.BucketName)})
	if err != nil {
		return nil, err
	}

	return objects.Contents, nil
}

func (s *Site) GetAllImageKeys() ([]string, error) {
	svc, err := s.GetS3Service()
	if err != nil {
		return nil, err
	}

	objects, err := s.GetAllObjects()
	if err != nil {
		return nil, err
	}

	var imageKeys []string
	var IMAGE_CONTENT_TYPES []string = []string{"image/jpeg", "image/png"}

	for _, obj := range objects {
		headOutput, err := svc.HeadObject(&s3.HeadObjectInput{
			Bucket: aws.String(s.BucketName),
			Key:    obj.Key,
		})
		if err != nil {
			continue
		}

		for _, CT := range IMAGE_CONTENT_TYPES {
			if CT == *headOutput.ContentType {
				imageKeys = append(imageKeys, *obj.Key)
			}
		}
	}

	return imageKeys, nil
}

func (s *Site) GetUrlForImage(key string) (string, error) {
	if s.UseImgix {
		return s.GetImgixUrl(key)
	} else {
		return s.GetAwsUrl(key)
	}
}

func (s *Site) GetAwsUrl(key string) (string, error) {
	svc, err := s.GetS3Service()
	if err != nil {
		return "", err
	}

	req, _ := svc.GetObjectRequest(&s3.GetObjectInput{
		Bucket: aws.String(s.BucketName),
		Key:    aws.String(key),
	})

	signedUrl, err := req.Presign(24 * time.Hour)
	if err != nil {
		return "", err
	}

	return signedUrl, nil
}

func (s *Site) GetImgixUrl(key string) (string, error) {
	baseUrl, err := url.Parse(s.BaseUrl)
	if err != nil {
		return "", err
	}

	keyPath, err := url.Parse(key)
	if err != nil {
		return "", err
	}

	return fmt.Sprintf("%s?w=800", baseUrl.ResolveReference(keyPath).String()), nil
}
<commit_msg>Removed check for Content Types as it took too long on larger buckets<commit_after>package main

import (
	"fmt"

	"time"

	"net/url"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/s3"
	"github.com/go-ini/ini"
)

type Site struct {
	Domain   string
	AuthUser string
	AuthPass string

	BucketRegion string
	BucketName   string

	UseImgix bool
	BaseUrl  string

	AWS_SECRET_KEY_ID string
	AWS_SECRET_KEY    string

	MetaTitle  string
	SiteTitle  string
	AlbumTitle string
}

func LoadSiteFromFile(path string) (*Site, error) {
	cfg, err := ini.Load(path)
	if err != nil {
		return nil, err
	}

	section, err := cfg.GetSection("")
	if err != nil {
		return nil, err
	}

	requiredFields := []string{
		"Domain", "Region", "Bucket", "UseImgix", "BaseUrl", "AWSKeyId", "AWSKey", "MetaTitle",
		"SiteTitle", "AlbumTitle",
	}
	for _, v := range requiredFields {
		if !section.HasKey(v) {
			return nil, fmt.Errorf("Config file %s does not contain value of required key %s", path, v)
		}
	}

	bucketName := section.Key("Bucket").String()
	s := &Site{
		Domain: section.Key("Domain").String(),

		BucketRegion: section.Key("Region").String(),
		BucketName:   bucketName,

		UseImgix: section.Key("UseImgix").String() == "1",
		BaseUrl:  section.Key("BaseUrl").String(),

		AWS_SECRET_KEY_ID: section.Key("AWSKeyId").String(),
		AWS_SECRET_KEY:    section.Key("AWSKey").String(),

		MetaTitle:  section.Key("MetaTitle").String(),
		SiteTitle:  section.Key("SiteTitle").String(),
		AlbumTitle: section.Key("AlbumTitle").String(),
	}

	if section.HasKey("AuthUser") && section.HasKey("AuthPass") {
		s.AuthUser = section.Key("AuthUser").String()
		s.AuthPass = section.Key("AuthPass").String()
	}

	return s, nil
}

func (s *Site) GetS3Service() (*s3.S3, error) {
	sess, err := session.NewSession(&aws.Config{
		Region:      aws.String(s.BucketRegion),
		Credentials: credentials.NewStaticCredentials(s.AWS_SECRET_KEY_ID, s.AWS_SECRET_KEY, ""),
	})

	if err != nil {
		return nil, err
	}

	return s3.New(sess), nil
}

func (s *Site) GetAllObjects() ([]*s3.Object, error) {
	svc, err := s.GetS3Service()
	if err != nil {
		return nil, err
	}

	objects, err := svc.ListObjects(&s3.ListObjectsInput{Bucket: aws.String(s.BucketName)})
	if err != nil {
		return nil, err
	}

	return objects.Contents, nil
}

func (s *Site) GetAllImageKeys() ([]string, error) {
	svc, err := s.GetS3Service()
	if err != nil {
		return nil, err
	}

	objects, err := s.GetAllObjects()
	if err != nil {
		return nil, err
	}

	var imageKeys []string
	for _, obj := range objects {
		imageKeys = append(imageKeys, *obj.Key)
	}
	return imageKeys, nil
}

func (s *Site) GetUrlForImage(key string) (string, error) {
	if s.UseImgix {
		return s.GetImgixUrl(key)
	} else {
		return s.GetAwsUrl(key)
	}
}

func (s *Site) GetAwsUrl(key string) (string, error) {
	svc, err := s.GetS3Service()
	if err != nil {
		return "", err
	}

	req, _ := svc.GetObjectRequest(&s3.GetObjectInput{
		Bucket: aws.String(s.BucketName),
		Key:    aws.String(key),
	})

	signedUrl, err := req.Presign(24 * time.Hour)
	if err != nil {
		return "", err
	}

	return signedUrl, nil
}

func (s *Site) GetImgixUrl(key string) (string, error) {
	baseUrl, err := url.Parse(s.BaseUrl)
	if err != nil {
		return "", err
	}

	keyPath, err := url.Parse(key)
	if err != nil {
		return "", err
	}

	return fmt.Sprintf("%s?w=800", baseUrl.ResolveReference(keyPath).String()), nil
}
<|endoftext|>


<commit_before>/*
Chevalier is a search engine package for Vaultaire sources. It is backed
to Elasticsearch.
*/
package chevalier

const (
	Version = "1.0.0alpha1"
)
<commit_msg>Version bump for protobuf change<commit_after>/*
Chevalier is a search engine package for Vaultaire sources. It is backed
to Elasticsearch.
*/
package chevalier

const (
	Version = "1.0.0alpha2"
)
<|endoftext|>


<commit_before>// A simple log wrapper for std log, support log level.
// usage reforence golang std log

package slog

import (
	"fmt"
	"io"
	stdLog "log"
	"os"
)

// Level is log level
type Level int

// log level definition
const (
	DEBUG = iota
	INFO
	NOTICE
	WARN
	ERROR
	FATAL
	_PANIC // need not access it
)

var levelName = []string{"[DEBUG] ", "[INFO] ", "[NOTICE] ", "[WARN] ", "[ERROR] ", "[FATAL] ", "[PANIC] "}

// From std log.
// These flags define which text to prefix to each log entry generated by the Logger.
const (
	// Bits or'ed together to control what's printed. There is no control over the
	// order they appear (the order listed here) or the format they present (as
	// described in the comments).  A colon appears after these items:
	//	2009/01/23 01:23:23.123123 /a/b/c/d.go:23: message
	Ldate         = 1 << iota     // the date: 2009/01/23
	Ltime                         // the time: 01:23:23
	Lmicroseconds                 // microsecond resolution: 01:23:23.123123.  assumes Ltime.
	Llongfile                     // full file name and line number: /a/b/c/d.go:23
	Lshortfile                    // final file name element and line number: d.go:23. overrides Llongfile
	LstdFlags     = Ldate | Ltime // initial values for the standard logger
)

type Slogger struct {
	logger *stdLog.Logger
	level  Level
	// log.Logger.Output callPath
	callPath int

	debugImpl   func(l *Slogger, level Level, v ...interface{})
	debugfImpl  func(l *Slogger, level Level, format string, v ...interface{})
	debuglnImpl func(l *Slogger, level Level, v ...interface{})

	infoImpl   func(l *Slogger, level Level, v ...interface{})
	infofImpl  func(l *Slogger, level Level, format string, v ...interface{})
	infolnImpl func(l *Slogger, level Level, v ...interface{})

	noticeImpl   func(l *Slogger, level Level, v ...interface{})
	noticefImpl  func(l *Slogger, level Level, format string, v ...interface{})
	noticelnImpl func(l *Slogger, level Level, v ...interface{})

	warnImpl   func(l *Slogger, level Level, v ...interface{})
	warnfImpl  func(l *Slogger, level Level, format string, v ...interface{})
	warnlnImpl func(l *Slogger, level Level, v ...interface{})

	errorImpl   func(l *Slogger, level Level, v ...interface{})
	errorfImpl  func(l *Slogger, level Level, format string, v ...interface{})
	errorlnImpl func(l *Slogger, level Level, v ...interface{})

	fatalImpl   func(l *Slogger, level Level, v ...interface{})
	fatalfImpl  func(l *Slogger, level Level, format string, v ...interface{})
	fatallnImpl func(l *Slogger, level Level, v ...interface{})

	panicImpl   func(l *Slogger, level Level, v ...interface{})
	panicfImpl  func(l *Slogger, level Level, format string, v ...interface{})
	paniclnImpl func(l *Slogger, level Level, v ...interface{})
}

// New will create a new Slogger object
func New(out io.Writer, level Level, prefix string, flag int) *Slogger {
	l := &Slogger{
		logger:   stdLog.New(out, prefix, flag),
		callPath: 3,
	}
	l.SetLevel(level)
	return l
}

// func suffix is "Y" is valid implements
// func suffix is "N" is empty implements

func printImplY(l *Slogger, level Level, v ...interface{}) {
	l.logger.Output(l.callPath, levelName[level]+fmt.Sprint(v...))
}

func printImplN(l *Slogger, level Level, v ...interface{}) {
}

func printfImplY(l *Slogger, level Level, format string, v ...interface{}) {
	l.logger.Output(l.callPath, fmt.Sprintf(levelName[level]+format, v...))
}

func printfImplN(l *Slogger, level Level, format string, v ...interface{}) {
}

func printlnImplY(l *Slogger, level Level, v ...interface{}) {
	l.logger.Output(l.callPath, levelName[level]+fmt.Sprintln(v...))
}

func printlnImplN(l *Slogger, level Level, v ...interface{}) {
}

func (l *Slogger) SetLevel(level Level) {
	l.level = level
	// init all print func
	l.debugImpl = printImplN
	l.debugfImpl = printfImplN
	l.debuglnImpl = printlnImplN

	l.infoImpl = printImplN
	l.infofImpl = printfImplN
	l.infolnImpl = printlnImplN

	l.noticeImpl = printImplN
	l.noticefImpl = printfImplN
	l.noticelnImpl = printlnImplN

	l.warnImpl = printImplN
	l.warnfImpl = printfImplN
	l.warnlnImpl = printlnImplN

	l.errorImpl = printImplN
	l.errorfImpl = printfImplN
	l.errorlnImpl = printlnImplN

	// log that level is PANIC and FATAL must be output
	switch level {
	case DEBUG:
		l.debugImpl = printImplY
		l.debugfImpl = printfImplY
		l.debuglnImpl = printlnImplY
		fallthrough
	case INFO:
		l.infoImpl = printImplY
		l.infofImpl = printfImplY
		l.infolnImpl = printlnImplY
		fallthrough
	case NOTICE:
		l.noticeImpl = printImplY
		l.noticefImpl = printfImplY
		l.noticelnImpl = printlnImplY
		fallthrough
	case WARN:
		l.warnImpl = printImplY
		l.warnfImpl = printfImplY
		l.warnlnImpl = printlnImplY
		fallthrough
	case ERROR:
		l.errorImpl = printImplY
		l.errorfImpl = printfImplY
		l.errorlnImpl = printlnImplY
	case _PANIC, FATAL:

	}
}

// Debug
func (l *Slogger) Debug(v ...interface{}) {
	var level Level = DEBUG
	l.debugImpl(l, level, v...)
}

func (l *Slogger) Debugf(format string, v ...interface{}) {
	var level Level = DEBUG
	l.debugfImpl(l, level, format, v...)
}

func (l *Slogger) Debugln(v ...interface{}) {
	var level Level = DEBUG
	l.debuglnImpl(l, level, v...)
}

// Info
func (l *Slogger) Info(v ...interface{}) {
	var level Level = INFO
	l.infoImpl(l, level, v...)
}

func (l *Slogger) Infof(format string, v ...interface{}) {
	var level Level = INFO
	l.infofImpl(l, level, format, v...)
}

func (l *Slogger) Infoln(v ...interface{}) {
	var level Level = INFO
	l.infolnImpl(l, level, v...)
}

// Notice
func (l *Slogger) Notice(v ...interface{}) {
	var level Level = NOTICE
	l.noticeImpl(l, level, v...)
}

func (l *Slogger) Noticef(format string, v ...interface{}) {
	var level Level = NOTICE
	l.noticefImpl(l, level, format, v...)
}

func (l *Slogger) Noticeln(v ...interface{}) {
	var level Level = NOTICE
	l.noticelnImpl(l, level, v...)
}

// Warn
func (l *Slogger) Warn(v ...interface{}) {
	var level Level = WARN
	l.warnImpl(l, level, v...)
}

func (l *Slogger) Warnf(format string, v ...interface{}) {
	var level Level = WARN
	l.warnfImpl(l, level, format, v...)
}

func (l *Slogger) Warnln(v ...interface{}) {
	var level Level = WARN
	l.warnlnImpl(l, level, v...)
}

// Error
func (l *Slogger) Error(v ...interface{}) {
	var level Level = ERROR
	l.errorImpl(l, level, v...)
}

func (l *Slogger) Errorf(format string, v ...interface{}) {
	var level Level = ERROR
	l.errorfImpl(l, level, format, v...)
}

func (l *Slogger) Errorln(v ...interface{}) {
	var level Level = ERROR
	l.errorlnImpl(l, level, v...)
}

// Fatal
func (l *Slogger) Fatal(v ...interface{}) {
	l.logger.Output(l.callPath, levelName[FATAL]+fmt.Sprint(v...))
	os.Exit(1)
}

func (l *Slogger) Fatalf(format string, v ...interface{}) {
	l.logger.Output(l.callPath, fmt.Sprintf(levelName[FATAL]+format, v...))
	os.Exit(1)
}

func (l *Slogger) Fatalln(v ...interface{}) {
	l.logger.Output(l.callPath, levelName[FATAL]+fmt.Sprintln(v...))
	os.Exit(1)
}

// Panic
func (l *Slogger) Panic(v ...interface{}) {
	s := levelName[_PANIC] + fmt.Sprintln(v...)
	l.logger.Output(l.callPath, s)
	panic(s)
}

func (l *Slogger) Panicf(format string, v ...interface{}) {
	s := fmt.Sprintf(levelName[_PANIC]+format, v...)
	l.logger.Output(l.callPath, s)
	panic(s)
}

func (l *Slogger) Panicln(v ...interface{}) {
	s := levelName[_PANIC] + fmt.Sprintln(v...)
	l.logger.Output(l.callPath, s)
	panic(s)
}

// default logger
var dlog *Slogger

func init() {
	dlog = New(os.Stderr, DEBUG, "", LstdFlags|Lshortfile)
	dlog.callPath = 4
}

func SetLevel(level Level) {
	dlog.SetLevel(level)
}

func Debug(v ...interface{}) {
	dlog.Debug(v...)
}

func Debugf(fmt string, v ...interface{}) {
	dlog.Debugf(fmt, v...)
}

func Debugln(v ...interface{}) {
	dlog.Debugln(v...)
}

func Info(v ...interface{}) {
	dlog.Info(v...)
}

func Infof(fmt string, v ...interface{}) {
	dlog.Infof(fmt, v...)
}

func Infoln(v ...interface{}) {
	dlog.Infoln(v...)
}
func Notice(v ...interface{}) {
	dlog.Notice(v...)
}

func Noticef(fmt string, v ...interface{}) {
	dlog.Noticef(fmt, v...)
}

func Noticeln(v ...interface{}) {
	dlog.Noticeln(v...)
}

func Warn(v ...interface{}) {
	dlog.Warn(v...)
}

func Warnf(fmt string, v ...interface{}) {
	dlog.Warnf(fmt, v...)
}

func Warnln(v ...interface{}) {
	dlog.Warnln(v...)
}

func Error(v ...interface{}) {
	dlog.Error(v...)
}

func Errorf(fmt string, v ...interface{}) {
	dlog.Errorf(fmt, v...)
}

func Errorln(v ...interface{}) {
	dlog.Errorln(v...)
}

func Fatal(v ...interface{}) {
	dlog.Fatal(v...)
}

func Fatalf(fmt string, v ...interface{}) {
	dlog.Fatalf(fmt, v...)
}

func Fatalln(v ...interface{}) {
	dlog.Fatalln(v...)
}

func Panic(v ...interface{}) {
	dlog.Panic(v...)
}

func Panicf(fmt string, v ...interface{}) {
	dlog.Panicf(fmt, v...)
}

func Panicln(v ...interface{}) {
	dlog.Panicln(v...)
}
<commit_msg>modify New interface<commit_after>// A simple log wrapper for std log, support log level.
// usage reforence golang std log

package slog

import (
	"fmt"
	"io"
	stdLog "log"
	"os"
)

// Level is log level
type Level int

// log level definition
const (
	DEBUG = iota
	INFO
	NOTICE
	WARN
	ERROR
	FATAL
	_PANIC // need not access it
)

var levelName = []string{"[DEBUG] ", "[INFO] ", "[NOTICE] ", "[WARN] ", "[ERROR] ", "[FATAL] ", "[PANIC] "}

// From std log.
// These flags define which text to prefix to each log entry generated by the Logger.
const (
	// Bits or'ed together to control what's printed. There is no control over the
	// order they appear (the order listed here) or the format they present (as
	// described in the comments).  A colon appears after these items:
	//	2009/01/23 01:23:23.123123 /a/b/c/d.go:23: message
	Ldate         = 1 << iota     // the date: 2009/01/23
	Ltime                         // the time: 01:23:23
	Lmicroseconds                 // microsecond resolution: 01:23:23.123123.  assumes Ltime.
	Llongfile                     // full file name and line number: /a/b/c/d.go:23
	Lshortfile                    // final file name element and line number: d.go:23. overrides Llongfile
	LstdFlags     = Ldate | Ltime // initial values for the standard logger
)

type Slogger struct {
	logger *stdLog.Logger
	level  Level
	// log.Logger.Output callPath
	callPath int

	debugImpl   func(l *Slogger, level Level, v ...interface{})
	debugfImpl  func(l *Slogger, level Level, format string, v ...interface{})
	debuglnImpl func(l *Slogger, level Level, v ...interface{})

	infoImpl   func(l *Slogger, level Level, v ...interface{})
	infofImpl  func(l *Slogger, level Level, format string, v ...interface{})
	infolnImpl func(l *Slogger, level Level, v ...interface{})

	noticeImpl   func(l *Slogger, level Level, v ...interface{})
	noticefImpl  func(l *Slogger, level Level, format string, v ...interface{})
	noticelnImpl func(l *Slogger, level Level, v ...interface{})

	warnImpl   func(l *Slogger, level Level, v ...interface{})
	warnfImpl  func(l *Slogger, level Level, format string, v ...interface{})
	warnlnImpl func(l *Slogger, level Level, v ...interface{})

	errorImpl   func(l *Slogger, level Level, v ...interface{})
	errorfImpl  func(l *Slogger, level Level, format string, v ...interface{})
	errorlnImpl func(l *Slogger, level Level, v ...interface{})

	fatalImpl   func(l *Slogger, level Level, v ...interface{})
	fatalfImpl  func(l *Slogger, level Level, format string, v ...interface{})
	fatallnImpl func(l *Slogger, level Level, v ...interface{})

	panicImpl   func(l *Slogger, level Level, v ...interface{})
	panicfImpl  func(l *Slogger, level Level, format string, v ...interface{})
	paniclnImpl func(l *Slogger, level Level, v ...interface{})
}

// New will create a new Slogger object
// level's value: "DEBUG", "INFO", "NOTICE", "WARN", "ERROR", "FATAL"
func New(out io.Writer, level string, prefix string, flag int) *Slogger {
	l := &Slogger{
		logger:   stdLog.New(out, prefix, flag),
		callPath: 3,
	}

	lv := parseLevel(level)
	l.SetLevel(lv)
	return l
}

func parseLevel(strLevel string) Level {
	switch strLevel {
	case "DEBUG":
		return DEBUG
	case "INFO":
		return INFO
	case "NOTICE":
		return NOTICE
	case "WARN":
		return WARN
	case "ERROR":
		return ERROR
	case "FATAL":
		return FATAL
	default:
		panic("unkown log level: " + strLevel)
	}
}

// func suffix is "Y" is valid implements
// func suffix is "N" is empty implements

func printImplY(l *Slogger, level Level, v ...interface{}) {
	l.logger.Output(l.callPath, levelName[level]+fmt.Sprint(v...))
}

func printImplN(l *Slogger, level Level, v ...interface{}) {
}

func printfImplY(l *Slogger, level Level, format string, v ...interface{}) {
	l.logger.Output(l.callPath, fmt.Sprintf(levelName[level]+format, v...))
}

func printfImplN(l *Slogger, level Level, format string, v ...interface{}) {
}

func printlnImplY(l *Slogger, level Level, v ...interface{}) {
	l.logger.Output(l.callPath, levelName[level]+fmt.Sprintln(v...))
}

func printlnImplN(l *Slogger, level Level, v ...interface{}) {
}

func (l *Slogger) SetLevel(level Level) {
	l.level = level
	// init all print func
	l.debugImpl = printImplN
	l.debugfImpl = printfImplN
	l.debuglnImpl = printlnImplN

	l.infoImpl = printImplN
	l.infofImpl = printfImplN
	l.infolnImpl = printlnImplN

	l.noticeImpl = printImplN
	l.noticefImpl = printfImplN
	l.noticelnImpl = printlnImplN

	l.warnImpl = printImplN
	l.warnfImpl = printfImplN
	l.warnlnImpl = printlnImplN

	l.errorImpl = printImplN
	l.errorfImpl = printfImplN
	l.errorlnImpl = printlnImplN

	// log that level is PANIC and FATAL must be output
	switch level {
	case DEBUG:
		l.debugImpl = printImplY
		l.debugfImpl = printfImplY
		l.debuglnImpl = printlnImplY
		fallthrough
	case INFO:
		l.infoImpl = printImplY
		l.infofImpl = printfImplY
		l.infolnImpl = printlnImplY
		fallthrough
	case NOTICE:
		l.noticeImpl = printImplY
		l.noticefImpl = printfImplY
		l.noticelnImpl = printlnImplY
		fallthrough
	case WARN:
		l.warnImpl = printImplY
		l.warnfImpl = printfImplY
		l.warnlnImpl = printlnImplY
		fallthrough
	case ERROR:
		l.errorImpl = printImplY
		l.errorfImpl = printfImplY
		l.errorlnImpl = printlnImplY
	case _PANIC, FATAL:

	}
}

// Debug
func (l *Slogger) Debug(v ...interface{}) {
	var level Level = DEBUG
	l.debugImpl(l, level, v...)
}

func (l *Slogger) Debugf(format string, v ...interface{}) {
	var level Level = DEBUG
	l.debugfImpl(l, level, format, v...)
}

func (l *Slogger) Debugln(v ...interface{}) {
	var level Level = DEBUG
	l.debuglnImpl(l, level, v...)
}

// Info
func (l *Slogger) Info(v ...interface{}) {
	var level Level = INFO
	l.infoImpl(l, level, v...)
}

func (l *Slogger) Infof(format string, v ...interface{}) {
	var level Level = INFO
	l.infofImpl(l, level, format, v...)
}

func (l *Slogger) Infoln(v ...interface{}) {
	var level Level = INFO
	l.infolnImpl(l, level, v...)
}

// Notice
func (l *Slogger) Notice(v ...interface{}) {
	var level Level = NOTICE
	l.noticeImpl(l, level, v...)
}

func (l *Slogger) Noticef(format string, v ...interface{}) {
	var level Level = NOTICE
	l.noticefImpl(l, level, format, v...)
}

func (l *Slogger) Noticeln(v ...interface{}) {
	var level Level = NOTICE
	l.noticelnImpl(l, level, v...)
}

// Warn
func (l *Slogger) Warn(v ...interface{}) {
	var level Level = WARN
	l.warnImpl(l, level, v...)
}

func (l *Slogger) Warnf(format string, v ...interface{}) {
	var level Level = WARN
	l.warnfImpl(l, level, format, v...)
}

func (l *Slogger) Warnln(v ...interface{}) {
	var level Level = WARN
	l.warnlnImpl(l, level, v...)
}

// Error
func (l *Slogger) Error(v ...interface{}) {
	var level Level = ERROR
	l.errorImpl(l, level, v...)
}

func (l *Slogger) Errorf(format string, v ...interface{}) {
	var level Level = ERROR
	l.errorfImpl(l, level, format, v...)
}

func (l *Slogger) Errorln(v ...interface{}) {
	var level Level = ERROR
	l.errorlnImpl(l, level, v...)
}

// Fatal
func (l *Slogger) Fatal(v ...interface{}) {
	l.logger.Output(l.callPath, levelName[FATAL]+fmt.Sprint(v...))
	os.Exit(1)
}

func (l *Slogger) Fatalf(format string, v ...interface{}) {
	l.logger.Output(l.callPath, fmt.Sprintf(levelName[FATAL]+format, v...))
	os.Exit(1)
}

func (l *Slogger) Fatalln(v ...interface{}) {
	l.logger.Output(l.callPath, levelName[FATAL]+fmt.Sprintln(v...))
	os.Exit(1)
}

// Panic
func (l *Slogger) Panic(v ...interface{}) {
	s := levelName[_PANIC] + fmt.Sprintln(v...)
	l.logger.Output(l.callPath, s)
	panic(s)
}

func (l *Slogger) Panicf(format string, v ...interface{}) {
	s := fmt.Sprintf(levelName[_PANIC]+format, v...)
	l.logger.Output(l.callPath, s)
	panic(s)
}

func (l *Slogger) Panicln(v ...interface{}) {
	s := levelName[_PANIC] + fmt.Sprintln(v...)
	l.logger.Output(l.callPath, s)
	panic(s)
}

// default logger
var dlog *Slogger

func init() {
	dlog = New(os.Stderr, "DEBUG", "", LstdFlags|Lshortfile)
	dlog.callPath = 4
}

func SetLevel(level Level) {
	dlog.SetLevel(level)
}

func Debug(v ...interface{}) {
	dlog.Debug(v...)
}

func Debugf(fmt string, v ...interface{}) {
	dlog.Debugf(fmt, v...)
}

func Debugln(v ...interface{}) {
	dlog.Debugln(v...)
}

func Info(v ...interface{}) {
	dlog.Info(v...)
}

func Infof(fmt string, v ...interface{}) {
	dlog.Infof(fmt, v...)
}

func Infoln(v ...interface{}) {
	dlog.Infoln(v...)
}
func Notice(v ...interface{}) {
	dlog.Notice(v...)
}

func Noticef(fmt string, v ...interface{}) {
	dlog.Noticef(fmt, v...)
}

func Noticeln(v ...interface{}) {
	dlog.Noticeln(v...)
}

func Warn(v ...interface{}) {
	dlog.Warn(v...)
}

func Warnf(fmt string, v ...interface{}) {
	dlog.Warnf(fmt, v...)
}

func Warnln(v ...interface{}) {
	dlog.Warnln(v...)
}

func Error(v ...interface{}) {
	dlog.Error(v...)
}

func Errorf(fmt string, v ...interface{}) {
	dlog.Errorf(fmt, v...)
}

func Errorln(v ...interface{}) {
	dlog.Errorln(v...)
}

func Fatal(v ...interface{}) {
	dlog.Fatal(v...)
}

func Fatalf(fmt string, v ...interface{}) {
	dlog.Fatalf(fmt, v...)
}

func Fatalln(v ...interface{}) {
	dlog.Fatalln(v...)
}

func Panic(v ...interface{}) {
	dlog.Panic(v...)
}

func Panicf(fmt string, v ...interface{}) {
	dlog.Panicf(fmt, v...)
}

func Panicln(v ...interface{}) {
	dlog.Panicln(v...)
}
<|endoftext|>


<commit_before>package main

import (
	"os/exec"
	"os"
	"io/ioutil"
	"gopkg.in/yaml.v2"
	"log"
	"strings"
	"bufio"
	"bytes"
	"time"
	"net"
)

type config struct {
	Mailfrom string `yaml:"mail_from"`
	Mailfromname string `yaml:"mail_from_name"`
	Mailsubject string `yaml:"mail_subject"`
	Mailtos []string `yaml:"mail_to"`
	Allowedaddresses []string `yaml:"allowed_addresses"`
	Rechecktime int64 `yaml:"recheck_time"`
	Gracetime int64 `yaml:"grace_time"`
}

func (c *config) getConfig(configfile string) *config {
	yamlFile, readErr := ioutil.ReadFile(configfile)
	if readErr != nil {
		log.Printf("yamlFile.Get err   #%v ", readErr)
		panic(readErr)
	}

	yamlErr := yaml.Unmarshal(yamlFile, &c)
	if yamlErr != nil {
		log.Fatalf("Unmarshal: %v", yamlErr)
		panic(yamlErr)
	}

	return c
}

func getWho() string {
	who, lookErr := exec.LookPath("who")
	if lookErr != nil {
		panic(lookErr)
	}

	args := []string{"-u"}

	whoOut, execErr := exec.Command(who, args...).Output();
	if execErr != nil {
		panic(execErr)
	}
	return string(whoOut)
}

func isNotAllowed(address string, allowedNetworks []string) bool {
	parsedAddress := net.ParseIP(address)
	if parsedAddress == nil {
		for _, allowedHostname := range allowedNetworks {
			if allowedHostname == address {
				return false
			}
		}
	} else {
		for _, allowedNetwork := range allowedNetworks {
			_, parsedAllowedNetworkCIDR, _ := net.ParseCIDR(allowedNetwork)
			if parsedAllowedNetworkCIDR == nil {
			} else {
				if parsedAllowedNetworkCIDR.Contains(parsedAddress) {
					return false
				}
			}
		}
	}
	return true
}

func hostname() string {
	hostname, lookErr := exec.LookPath("hostname")
	if lookErr != nil {
		panic(lookErr)
	}

	args := []string{"-f"}

	hostFqdn, execErr := exec.Command(hostname, args...).Output();
	if execErr != nil {
		panic(execErr)
	}

	return string(hostFqdn)
}

func sendMail(recipient string, mailfrom string, mailfromname string, mailsubject string, mailbody string) {
	var sendmailBuffer bytes.Buffer
	sendmailBuffer.WriteString("Subject: ")
	sendmailBuffer.WriteString(mailsubject)
	sendmailBuffer.WriteString("\nLoginchecker triggered an alert\n")
	sendmailBuffer.WriteString("\n\n")
	sendmailBuffer.WriteString(mailbody)
	sendmailBuffer.WriteString("\n.")

	mail, lookErr := exec.LookPath("sendmail")
	if lookErr != nil {
		panic(lookErr)
	}

	args := []string{"-t", "-f", mailfrom, "-F", mailfromname, recipient}

	sendmail := exec.Command(mail, args...);
	sendmail.Stdin = strings.NewReader(sendmailBuffer.String())
	execErr := sendmail.Run()
	if execErr != nil {
		panic(execErr)
	}
}

func main () {
	var (
		config config
	)

	configfile := "./config.yml"
	if  len(os.Args) > 1 {
		arg := os.Args[1]
		configfile = arg
	}

	config.getConfig(configfile)
	recheckTime := time.Duration(config.Rechecktime) * time.Second
	graceTime := time.Duration(config.Gracetime) * time.Second
	alertMailtos := config.Mailtos
	allowedAddresses := config.Allowedaddresses
	mailFrom := config.Mailfrom
	mailFromName := config.Mailfromname
	mailSubject := config.Mailsubject + " @ " + hostname()

	logged := make(map[string]time.Time)

	for {
		who := getWho()
		scanner := bufio.NewScanner(strings.NewReader(who))
		for scanner.Scan() {
			line := scanner.Text()
			whoIsLoggedIn := strings.Fields(line)
			username := whoIsLoggedIn[0]
			address := strings.Trim(whoIsLoggedIn[6], "(,)")
                        uniqeUser := username + address

			if isNotAllowed(address, allowedAddresses) {
				sendmail := false

				timeDetected := time.Now()
				if timeLogged, ok := logged[uniqeUser]; ok {
					if (timeDetected.Sub(timeLogged) > graceTime) {
						logged[uniqeUser] = timeDetected
						sendmail = true
					}
				} else {
					logged[uniqeUser] = timeDetected
					sendmail = true
				}

				if sendmail {
					mailBody := username + " is not allowed from " + address
					for _, alertMailto := range alertMailtos {
						sendMail(alertMailto, mailFrom, mailFromName, mailSubject, mailBody)
					}
				}
			}
		}
		time.Sleep(recheckTime)
	}
}<commit_msg>add check for screen session<commit_after>package main

import (
	"os/exec"
	"os"
	"io/ioutil"
	"gopkg.in/yaml.v2"
	"log"
	"strings"
	"bufio"
	"bytes"
	"time"
	"net"
)

type config struct {
	Mailfrom string `yaml:"mail_from"`
	Mailfromname string `yaml:"mail_from_name"`
	Mailsubject string `yaml:"mail_subject"`
	Mailtos []string `yaml:"mail_to"`
	Allowedaddresses []string `yaml:"allowed_addresses"`
	Rechecktime int64 `yaml:"recheck_time"`
	Gracetime int64 `yaml:"grace_time"`
}

func (c *config) getConfig(configfile string) *config {
	yamlFile, readErr := ioutil.ReadFile(configfile)
	if readErr != nil {
		log.Printf("yamlFile.Get err   #%v ", readErr)
		panic(readErr)
	}

	yamlErr := yaml.Unmarshal(yamlFile, &c)
	if yamlErr != nil {
		log.Fatalf("Unmarshal: %v", yamlErr)
		panic(yamlErr)
	}

	return c
}

func getWho() string {
	who, lookErr := exec.LookPath("who")
	if lookErr != nil {
		panic(lookErr)
	}

	args := []string{"-u"}

	whoOut, execErr := exec.Command(who, args...).Output();
	if execErr != nil {
		panic(execErr)
	}
	return string(whoOut)
}

func isNotAllowed(address string, allowedNetworks []string) bool {
	parsedAddress := net.ParseIP(address)
	if parsedAddress == nil {
		for _, allowedHostname := range allowedNetworks {
			if allowedHostname == address {
				return false
			}
		}
	} else {
		for _, allowedNetwork := range allowedNetworks {
			_, parsedAllowedNetworkCIDR, _ := net.ParseCIDR(allowedNetwork)
			if parsedAllowedNetworkCIDR == nil {
			} else {
				if parsedAllowedNetworkCIDR.Contains(parsedAddress) {
					return false
				}
			}
		}
	}
	return true
}

func hostname() string {
	hostname, lookErr := exec.LookPath("hostname")
	if lookErr != nil {
		panic(lookErr)
	}

	args := []string{"-f"}

	hostFqdn, execErr := exec.Command(hostname, args...).Output();
	if execErr != nil {
		panic(execErr)
	}

	return string(hostFqdn)
}

func sendMail(recipient string, mailfrom string, mailfromname string, mailsubject string, mailbody string) {
	var sendmailBuffer bytes.Buffer
	sendmailBuffer.WriteString("Subject: ")
	sendmailBuffer.WriteString(mailsubject)
	sendmailBuffer.WriteString("\nLoginchecker triggered an alert\n")
	sendmailBuffer.WriteString("\n\n")
	sendmailBuffer.WriteString(mailbody)
	sendmailBuffer.WriteString("\n.")

	mail, lookErr := exec.LookPath("sendmail")
	if lookErr != nil {
		panic(lookErr)
	}

	args := []string{"-t", "-f", mailfrom, "-F", mailfromname, recipient}

	sendmail := exec.Command(mail, args...);
	sendmail.Stdin = strings.NewReader(sendmailBuffer.String())
	execErr := sendmail.Run()
	if execErr != nil {
		panic(execErr)
	}
}

func main () {
	var (
		config config
	)

	configfile := "./config.yml"
	if  len(os.Args) > 1 {
		arg := os.Args[1]
		configfile = arg
	}

	config.getConfig(configfile)
	recheckTime := time.Duration(config.Rechecktime) * time.Second
	graceTime := time.Duration(config.Gracetime) * time.Second
	alertMailtos := config.Mailtos
	allowedAddresses := config.Allowedaddresses
	mailFrom := config.Mailfrom
	mailFromName := config.Mailfromname
	mailSubject := config.Mailsubject + " @ " + hostname()

	logged := make(map[string]time.Time)

	for {
		who := getWho()
		scanner := bufio.NewScanner(strings.NewReader(who))
		for scanner.Scan() {
			line := scanner.Text()
			whoIsLoggedIn := strings.Fields(line)
			username := whoIsLoggedIn[0]
			address := strings.Trim(whoIsLoggedIn[6], "(,)")
			if strings.Contains(address, ":S") {
				address = string(strings.Split(address, ":")[0])
			}

			println(address)
                        uniqeUser := username + address

			if isNotAllowed(address, allowedAddresses) {
				sendmail := false

				timeDetected := time.Now()
				if timeLogged, ok := logged[uniqeUser]; ok {
					if (timeDetected.Sub(timeLogged) > graceTime) {
						logged[uniqeUser] = timeDetected
						sendmail = true
					}
				} else {
					logged[uniqeUser] = timeDetected
					sendmail = true
				}

				if sendmail {
					mailBody := username + " is not allowed from " + address
					for _, alertMailto := range alertMailtos {
						sendMail(alertMailto, mailFrom, mailFromName, mailSubject, mailBody)
					}
				}
			}
		}
		time.Sleep(recheckTime)
	}
}<|endoftext|>


<commit_before>package bsd

import (
	"database/sql"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"path"
	"sort"
	"strings"
	"sync"
	"time"

	"github.com/lflux/eve-sdeloader/utils"
	"github.com/lib/pq"
	"gopkg.in/yaml.v2"
)

type Importer struct {
	DB            *sql.DB
	dontLowerCase bool
}

type invPosition struct {
	ItemID int64 `yaml:"itemID"`
	Pitch  *float64
	Yaw    *float64
	Roll   *float64
	X      float64
	Y      float64
	Z      float64
}

type importFunc func(db *sql.DB, r io.Reader) error

var importMutex sync.Mutex
var specificImporters = map[string]importFunc{
	"dgmTypeAttributes.yaml": importDgmTypeAttributes,
	"invNames.yaml":          importInvNames,
	"invItems.yaml":          importInvItems,
	"invPositions.yaml":      importInvPositions,
	"invUniqueNames.yaml":    importInvUniqueNames,
}

func (i *Importer) fixPostgresColumns(colname string) string {
	if !i.dontLowerCase {
		colname = strings.ToLower(colname)

		switch colname {
		case "zmax":
			return "z_max"
		case "zmin":
			return "z_min"
		case "xmax":
			return "x_max"
		case "xmin":
			return "x_min"
		case "ymax":
			return "y_max"
		case "ymin":
			return "y_min"
		default:
			return colname
		}
	}
	return colname
}

func (i *Importer) statement(statements map[string]*sql.Stmt, tx *sql.Tx, tableName string, keys []string) (*sql.Stmt, error) {
	splattedKeys := strings.Join(keys, ",")

	stmt, ok := statements[splattedKeys]
	if ok {
		return stmt, nil
	}
	var placeholders string
	for idx := range keys {
		if idx > 0 {
			placeholders += ", "
		}
		placeholders += fmt.Sprintf("$%d", idx+1)
	}

	query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)",
		pq.QuoteIdentifier(tableName),
		splattedKeys,
		placeholders,
	)
	// TODO cache statements
	stmt, err := tx.Prepare(query)

	if err != nil {
		log.Printf("Error preparing query  `%s`: %s", query, err)
		return nil, err
	}
	statements[splattedKeys] = stmt
	return stmt, nil
}

func (i *Importer) importToTable(tableName string, r io.Reader) error {
	statements := make(map[string]*sql.Stmt)

	buf, err := ioutil.ReadAll(r)
	if err != nil {
		return err
	}

	rows := make([]map[string]string, 0)
	err = yaml.Unmarshal(buf, &rows)
	if err != nil {
		return err
	}

	tx, err := i.DB.Begin()
	if err != nil {
		return err
	}

	for _, row := range rows {
		keys := make([]string, 0, len(row))
		dbKeys := make([]string, 0, len(row))
		vals := make([]interface{}, 0, len(row))

		for k := range row {
			keys = append(keys, k)
		}

		sort.Strings(keys)
		for _, k := range keys {
			v := row[k]
			key := i.fixPostgresColumns(k)
			dbKeys = append(dbKeys, pq.QuoteIdentifier(key))
			vals = append(vals, v)
		}
		stmt, err := i.statement(statements, tx, tableName, dbKeys)
		if err != nil {
			return err
		}
		_, err = stmt.Exec(vals...)
		if err != nil {
			log.Printf("Error executing insert on table %s for vals: %v", tableName, vals)
			return err
		}
	}

	for _, s := range statements {
		s.Close()
	}
	return tx.Commit()
}

func (i *Importer) importFile(root, fileName string) error {
	parts := strings.Split(fileName, ".")
	tableName := parts[0]
	if !i.dontLowerCase {
		tableName = strings.ToLower(tableName)
	}
	s := fmt.Sprintf("Importing %s into %s", fileName, tableName)
	log.Println(s)
	defer utils.TimeTrack(time.Now(), s)
	f, err := os.Open(path.Join(root, fileName))
	if err != nil {
		return err
	}

	importMutex.Lock()
	fn, ok := specificImporters[fileName]
	importMutex.Unlock()
	if ok {
		err = fn(i.DB, f)
	} else {
		err = i.importToTable(tableName, f)
	}
	if err != nil {
		return err
	}

	return nil
}

func (i *Importer) Import(root, singleFile string) error {
	defer utils.TimeTrack(time.Now(), "BSD Import")

	if i.DB == nil {
		return errors.New("Nil database pointer")
	}
	i.dontLowerCase = true
	files, err := ioutil.ReadDir(root)
	if err != nil {
		return err
	}
	var wg sync.WaitGroup

	for _, file := range files {
		wg.Add(1)
		go func(fileName string) {
			defer wg.Done()
			err = i.importFile(root, fileName)
			if err != nil {
				log.Println(err)
			}
		}(file.Name())
	}

	wg.Wait()
	return nil
}
<commit_msg>Remove todo and cut log spam<commit_after>package bsd

import (
	"database/sql"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"path"
	"sort"
	"strings"
	"sync"
	"time"

	"github.com/lflux/eve-sdeloader/utils"
	"github.com/lib/pq"
	"gopkg.in/yaml.v2"
)

type Importer struct {
	DB            *sql.DB
	dontLowerCase bool
}

type invPosition struct {
	ItemID int64 `yaml:"itemID"`
	Pitch  *float64
	Yaw    *float64
	Roll   *float64
	X      float64
	Y      float64
	Z      float64
}

type importFunc func(db *sql.DB, r io.Reader) error

var importMutex sync.Mutex
var specificImporters = map[string]importFunc{
	"dgmTypeAttributes.yaml": importDgmTypeAttributes,
	"invNames.yaml":          importInvNames,
	"invItems.yaml":          importInvItems,
	"invPositions.yaml":      importInvPositions,
	"invUniqueNames.yaml":    importInvUniqueNames,
}

func (i *Importer) fixPostgresColumns(colname string) string {
	if !i.dontLowerCase {
		colname = strings.ToLower(colname)

		switch colname {
		case "zmax":
			return "z_max"
		case "zmin":
			return "z_min"
		case "xmax":
			return "x_max"
		case "xmin":
			return "x_min"
		case "ymax":
			return "y_max"
		case "ymin":
			return "y_min"
		default:
			return colname
		}
	}
	return colname
}

func (i *Importer) statement(statements map[string]*sql.Stmt, tx *sql.Tx, tableName string, keys []string) (*sql.Stmt, error) {
	splattedKeys := strings.Join(keys, ",")

	stmt, ok := statements[splattedKeys]
	if ok {
		return stmt, nil
	}
	var placeholders string
	for idx := range keys {
		if idx > 0 {
			placeholders += ", "
		}
		placeholders += fmt.Sprintf("$%d", idx+1)
	}

	query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)",
		pq.QuoteIdentifier(tableName),
		splattedKeys,
		placeholders,
	)
	stmt, err := tx.Prepare(query)

	if err != nil {
		log.Printf("Error preparing query  `%s`: %s", query, err)
		return nil, err
	}
	statements[splattedKeys] = stmt
	return stmt, nil
}

func (i *Importer) importToTable(tableName string, r io.Reader) error {
	statements := make(map[string]*sql.Stmt)

	buf, err := ioutil.ReadAll(r)
	if err != nil {
		return err
	}

	rows := make([]map[string]string, 0)
	err = yaml.Unmarshal(buf, &rows)
	if err != nil {
		return err
	}

	tx, err := i.DB.Begin()
	if err != nil {
		return err
	}

	for _, row := range rows {
		keys := make([]string, 0, len(row))
		dbKeys := make([]string, 0, len(row))
		vals := make([]interface{}, 0, len(row))

		for k := range row {
			keys = append(keys, k)
		}

		sort.Strings(keys)
		for _, k := range keys {
			v := row[k]
			key := i.fixPostgresColumns(k)
			dbKeys = append(dbKeys, pq.QuoteIdentifier(key))
			vals = append(vals, v)
		}
		stmt, err := i.statement(statements, tx, tableName, dbKeys)
		if err != nil {
			return err
		}
		_, err = stmt.Exec(vals...)
		if err != nil {
			log.Printf("Error executing insert on table %s for vals: %v", tableName, vals)
			return err
		}
	}

	for _, s := range statements {
		s.Close()
	}
	return tx.Commit()
}

func (i *Importer) importFile(root, fileName string) error {
	parts := strings.Split(fileName, ".")
	tableName := parts[0]
	if !i.dontLowerCase {
		tableName = strings.ToLower(tableName)
	}
	s := fmt.Sprintf("Importing %s into %s", fileName, tableName)
	defer utils.TimeTrack(time.Now(), s)
	f, err := os.Open(path.Join(root, fileName))
	if err != nil {
		return err
	}

	importMutex.Lock()
	fn, ok := specificImporters[fileName]
	importMutex.Unlock()
	if ok {
		err = fn(i.DB, f)
	} else {
		err = i.importToTable(tableName, f)
	}
	if err != nil {
		return err
	}

	return nil
}

func (i *Importer) Import(root, singleFile string) error {
	defer utils.TimeTrack(time.Now(), "BSD Import")

	if i.DB == nil {
		return errors.New("Nil database pointer")
	}
	i.dontLowerCase = true
	files, err := ioutil.ReadDir(root)
	if err != nil {
		return err
	}
	var wg sync.WaitGroup

	for _, file := range files {
		wg.Add(1)
		go func(fileName string) {
			defer wg.Done()
			err = i.importFile(root, fileName)
			if err != nil {
				log.Println(err)
			}
		}(file.Name())
	}

	wg.Wait()
	return nil
}
<|endoftext|>


<commit_before>package snmp

import (
	"bytes"
	"errors"
	"log"
	"math/rand"
	"net"
	"strings"
	"sync"
	"time"
)

// Session represents an SNMP v3 session to a single device.
type Session struct {
	addr           *net.UDPAddr
	conn           *net.UDPConn
	inbound        chan []byte
	user           []byte
	authPassphrase []byte
	privPassphrase []byte

	engineID    []byte
	engineBoots int32
	engineTime  int32

	authKey []byte
	privKey []byte
	aesIV   int64

	inflight map[int]chan DataType
	lock     sync.Mutex
}

// newSession creates a new SNMP v3 session using "authPriv" mode with
// SHA authentication and AES encryption.
func newSession(address, user, authPassphrase, privPassphrase string) (*Session, error) {
	addr, err := net.ResolveUDPAddr("udp", address)
	if err != nil {
		return nil, err
	}

	sess := &Session{
		addr:           addr,
		inbound:        make(chan []byte),
		user:           []byte(user),
		authPassphrase: []byte(authPassphrase),
		privPassphrase: []byte(privPassphrase),
		aesIV:          rand.Int63(),
		inflight:       make(map[int]chan DataType),
	}

	go sess.handleListen()

	return sess, nil
}

func (s *Session) doRequest(data []byte, reqId int, c chan DataType) {
	_, err := s.conn.WriteTo(data, s.addr)
	if err != nil {
		close(c)
		return
	}

	s.lock.Lock()
	s.inflight[reqId] = c
	s.lock.Unlock()

	go func() {
		<-time.After(300 * time.Millisecond) // TODO: make this into a package-level variable
		s.lock.Lock()
		defer s.lock.Unlock()

		// TODO: explain what this is doing
		if c, ok := s.inflight[reqId]; ok {
			// haven't received a response yet

			close(c)
			delete(s.inflight, reqId)
		}
	}()
}

func (s *Session) doRequestStuff(packet []byte, requestID int) (DataType, error) {
	var decoded DataType
	var ok bool

	for i := 0; i < 10; i++ {
		c := make(chan DataType)
		s.doRequest(packet, requestID, c)

		decoded, ok = <-c
		if ok {
			break
		} else {
			if i == 2 {
				return nil, errors.New("snmp: timeout")
			}
		}
	}

	seq, ok := decoded.(Sequence)
	if !ok || len(seq) < 4 {
		return nil, errors.New("snmp: invalid response")
	}

	encrypted, ok := seq[3].(String)
	if !ok {
		return nil, errors.New("snmp: invalid encrypted contents")
	}

	engineData, ok := seq[2].(String)
	if !ok {
		return nil, errors.New("snmp: invalid engine data contents")
	}

	engineStuff, _, err := decode(bytes.NewReader([]byte(engineData)))

	if err != nil {
		return nil, err
	}

	engineSeq, ok := engineStuff.(Sequence)
	if !ok || len(engineSeq) < 6 {
		return nil, errors.New("snmp: invalid engine sequence")
	}

	boots, ok := engineSeq[1].(Int)
	if !ok {
		return nil, errors.New("snmp: invalid engine boots")
	}

	s.engineBoots = int32(boots)

	engineTime, ok := engineSeq[2].(Int)
	if !ok {
		return nil, errors.New("snmp: invalid engine time")
	}

	s.engineTime = int32(engineTime)

	priv, ok := engineSeq[5].(String)
	if !ok {
		return nil, errors.New("snmp: invalid priv")
	}

	result, _, err := decode(bytes.NewReader(s.decrypt([]byte(encrypted), []byte(priv))))

	resultSeq, ok := result.(Sequence)
	if !ok || len(resultSeq) < 3 {
		return nil, errors.New("snmp: invalid result sequence")
	}

	return resultSeq[2], nil
}

// TODO: add comment
func (s *Session) handleListen() {
	for buf := range s.inbound {
		decoded, _, err := decode(bytes.NewReader(buf))
		if err != nil {
			log.Println(err)
			continue
		}

		s.lock.Lock()

		// TODO: make this safe
		reqId := int(decoded.(Sequence)[1].(Sequence)[0].(Int))

		// TODO: make this safe
		switch decoded.(Sequence)[3].(type) {
		case String:
			encrypted := []byte(decoded.(Sequence)[3].(String)) // TODO: make this safe

			engineStuff, _, err := decode(bytes.NewReader([]byte(decoded.(Sequence)[2].(String))))
			if err != nil {
				continue
			}

			s.engineBoots = int32(engineStuff.(Sequence)[1].(Int))
			s.engineTime = int32(engineStuff.(Sequence)[2].(Int))

			priv := []byte(engineStuff.(Sequence)[5].(String))

			result, _, err := decode(bytes.NewReader(s.decrypt(encrypted, priv)))

			if err != nil {
				log.Println(err)
				continue
			}

			responseData := result.(Sequence)[2]

			switch responseData.(type) {
			case GetResponse:
				reqId = responseData.(GetResponse).requestID

			case Report:
				reqId = int(responseData.(Report)[0].(Int)) // TODO: ^
			}
		}

		if c, ok := s.inflight[reqId]; ok {
			c <- decoded // TODO: consider a non-blocking send? Think about deadlocks...
			delete(s.inflight, reqId)
		}

		s.lock.Unlock() // TODO: Non-blocking send might be better.
	}

}

// TODO: add comment
func (s *Session) Discover() error {
	reqId := int(rand.Intn(100000))

	encodedEngineData, err := Sequence{
		String(""),
		Int(0),
		Int(0),
		String(""),
		String(""),
		String(""),
	}.Encode()

	if err != nil {
		return err
	}

	discoverySequence, err := Sequence{
		Int(3),
		Sequence{
			Int(reqId),
			Int(65507),
			String("\x04"), // TODO: \x04?
			Int(3),
		},
		String(encodedEngineData),
		Sequence{
			String(""),
			String(""),
			newGetRequest(reqId, []Varbind{}),
		},
	}.Encode()

	if err != nil {
		return err
	}

	var decoded DataType
	var ok bool

	// TODO: make num of retries a package-level variable
	// TODO: turn this into a function?
	for i := 0; i < 3; i++ {
		c := make(chan DataType)
		s.doRequest(discoverySequence, int(reqId), c)

		decoded, ok = <-c
		if ok {
			break
		} else {
			if i == 2 {
				return errors.New("discovery failed")
			}
		}
	}

	engineStuff, _, err := decode(bytes.NewReader([]byte(decoded.(Sequence)[2].(String))))
	if err != nil {
		return err
	}

	s.engineID = []byte(engineStuff.(Sequence)[0].(String))
	s.engineBoots = int32(engineStuff.(Sequence)[1].(Int))
	s.engineTime = int32(engineStuff.(Sequence)[2].(Int))

	s.privKey = passphraseToKey(s.privPassphrase, s.engineID)[:16]
	s.authKey = passphraseToKey(s.authPassphrase, s.engineID)

	return nil
}

func (s *Session) Get(oid ObjectIdentifier) (*GetResponse, error) {
	reqId := int(rand.Int31())

	getReq, err := Sequence{
		String(s.engineID),
		String(""),
		newGetRequest(reqId, []Varbind{NewVarbind(oid, Null)}),
	}.Encode()

	if err != nil {
		return nil, err
	}

	// TODO: turn this all into a function
	encrypted, priv := s.encrypt(getReq)

	packet, err := s.constructPacket(encrypted, priv)
	if err != nil {
		return nil, err
	}

	result, err := s.doRequestStuff(packet, reqId)
	if err != nil {
		return nil, err
	}

	getRes, ok := result.(GetResponse)
	if !ok {
		return nil, ErrDecodingType
	}

	if len(getRes.varbinds) > 0 && getRes.varbinds[0].value == NoSuchInstance {
		return nil, errors.New("No Such Instance")
	}

	return &getRes, nil
}

func (s *Session) GetNext(oid ObjectIdentifier) (*GetResponse, error) {
	reqId := int(rand.Int31())

	getNextReq, err := Sequence{
		String(s.engineID),
		String(""),
		newGetNextRequest(reqId, []Varbind{NewVarbind(oid, Null)}),
	}.Encode()

	if err != nil {
		return nil, err
	}

	encrypted, priv := s.encrypt(getNextReq)

	packet, err := s.constructPacket(encrypted, priv)
	if err != nil {
		return nil, err
	}

	result, err := s.doRequestStuff(packet, reqId)
	if err != nil {
		return nil, err
	}

	getRes, ok := result.(GetResponse)
	if !ok {
		return nil, ErrDecodingType
	}

	if len(getRes.varbinds) > 0 && getRes.varbinds[0].value == EndOfMIBView {
		return nil, errors.New("End of MIB View")
	}

	return &getRes, nil
}

func (s *Session) constructPacket(encrypted, priv []byte) ([]byte, error) {
	msgId := Int(rand.Int31())

	v3Header, err := Sequence{
		String(s.engineID),
		Int(s.engineBoots),
		Int(s.engineTime),
		String(s.user),
		String(strings.Repeat("\x00", 12)),
		String(priv),
	}.Encode()

	if err != nil {
		return nil, err
	}

	packet, err := Sequence{
		Int(3),
		Sequence{
			msgId,
			Int(65507),
			String("\x07"),
			Int(3),
		},
		String(v3Header),
		String(encrypted),
	}.Encode()

	if err != nil {
		return nil, err
	}

	authParam := s.auth(packet)

	return bytes.Replace(packet, bytes.Repeat([]byte{0}, 12), authParam, 1), nil
}
<commit_msg>rename to get rid of "Stuff" in the code; #7<commit_after>package snmp

import (
	"bytes"
	"errors"
	"log"
	"math/rand"
	"net"
	"strings"
	"sync"
	"time"
)

// Session represents an SNMP v3 session to a single device.
type Session struct {
	addr           *net.UDPAddr
	conn           *net.UDPConn
	inbound        chan []byte
	user           []byte
	authPassphrase []byte
	privPassphrase []byte

	engineID    []byte
	engineBoots int32
	engineTime  int32

	authKey []byte
	privKey []byte
	aesIV   int64

	inflight map[int]chan DataType
	lock     sync.Mutex
}

// newSession creates a new SNMP v3 session using "authPriv" mode with
// SHA authentication and AES encryption.
func newSession(address, user, authPassphrase, privPassphrase string) (*Session, error) {
	addr, err := net.ResolveUDPAddr("udp", address)
	if err != nil {
		return nil, err
	}

	sess := &Session{
		addr:           addr,
		inbound:        make(chan []byte),
		user:           []byte(user),
		authPassphrase: []byte(authPassphrase),
		privPassphrase: []byte(privPassphrase),
		aesIV:          rand.Int63(),
		inflight:       make(map[int]chan DataType),
	}

	go sess.handleListen()

	return sess, nil
}

func (s *Session) sendRequest(data []byte, reqId int, c chan DataType) {
	_, err := s.conn.WriteTo(data, s.addr)
	if err != nil {
		close(c)
		return
	}

	s.lock.Lock()
	s.inflight[reqId] = c
	s.lock.Unlock()

	go func() {
		<-time.After(300 * time.Millisecond) // TODO: make this into a package-level variable
		s.lock.Lock()
		defer s.lock.Unlock()

		// TODO: explain what this is doing
		if c, ok := s.inflight[reqId]; ok {
			// haven't received a response yet

			close(c)
			delete(s.inflight, reqId)
		}
	}()
}

func (s *Session) doRequest(packet []byte, requestID int) (DataType, error) {
	var decoded DataType
	var ok bool

	for i := 0; i < 10; i++ {
		c := make(chan DataType)
		s.sendRequest(packet, requestID, c)

		decoded, ok = <-c
		if ok {
			break
		} else {
			if i == 2 {
				return nil, errors.New("snmp: timeout")
			}
		}
	}

	seq, ok := decoded.(Sequence)
	if !ok || len(seq) < 4 {
		return nil, errors.New("snmp: invalid response")
	}

	encrypted, ok := seq[3].(String)
	if !ok {
		return nil, errors.New("snmp: invalid encrypted contents")
	}

	engineData, ok := seq[2].(String)
	if !ok {
		return nil, errors.New("snmp: invalid engine data contents")
	}

	decodedEngineData, _, err := decode(bytes.NewReader([]byte(engineData)))

	if err != nil {
		return nil, err
	}

	engineSeq, ok := decodedEngineData.(Sequence)
	if !ok || len(engineSeq) < 6 {
		return nil, errors.New("snmp: invalid engine sequence")
	}

	boots, ok := engineSeq[1].(Int)
	if !ok {
		return nil, errors.New("snmp: invalid engine boots")
	}

	s.engineBoots = int32(boots)

	engineTime, ok := engineSeq[2].(Int)
	if !ok {
		return nil, errors.New("snmp: invalid engine time")
	}

	s.engineTime = int32(engineTime)

	priv, ok := engineSeq[5].(String)
	if !ok {
		return nil, errors.New("snmp: invalid priv")
	}

	result, _, err := decode(bytes.NewReader(s.decrypt([]byte(encrypted), []byte(priv))))

	resultSeq, ok := result.(Sequence)
	if !ok || len(resultSeq) < 3 {
		return nil, errors.New("snmp: invalid result sequence")
	}

	return resultSeq[2], nil
}

// TODO: add comment
func (s *Session) handleListen() {
	for buf := range s.inbound {
		decoded, _, err := decode(bytes.NewReader(buf))
		if err != nil {
			log.Println(err)
			continue
		}

		s.lock.Lock()

		// TODO: make this safe
		reqId := int(decoded.(Sequence)[1].(Sequence)[0].(Int))

		// TODO: make this safe
		switch decoded.(Sequence)[3].(type) {
		case String:
			encrypted := []byte(decoded.(Sequence)[3].(String)) // TODO: make this safe

			decodedEngineData, _, err := decode(bytes.NewReader([]byte(decoded.(Sequence)[2].(String))))
			if err != nil {
				continue
			}

			s.engineBoots = int32(decodedEngineData.(Sequence)[1].(Int))
			s.engineTime = int32(decodedEngineData.(Sequence)[2].(Int))

			priv := []byte(decodedEngineData.(Sequence)[5].(String))

			result, _, err := decode(bytes.NewReader(s.decrypt(encrypted, priv)))

			if err != nil {
				log.Println(err)
				continue
			}

			responseData := result.(Sequence)[2]

			switch responseData.(type) {
			case GetResponse:
				reqId = responseData.(GetResponse).requestID

			case Report:
				reqId = int(responseData.(Report)[0].(Int)) // TODO: ^
			}
		}

		if c, ok := s.inflight[reqId]; ok {
			c <- decoded // TODO: consider a non-blocking send? Think about deadlocks...
			delete(s.inflight, reqId)
		}

		s.lock.Unlock() // TODO: Non-blocking send might be better.
	}

}

// TODO: add comment
func (s *Session) Discover() error {
	reqId := int(rand.Intn(100000))

	encodedEngineData, err := Sequence{
		String(""),
		Int(0),
		Int(0),
		String(""),
		String(""),
		String(""),
	}.Encode()

	if err != nil {
		return err
	}

	discoverySequence, err := Sequence{
		Int(3),
		Sequence{
			Int(reqId),
			Int(65507),
			String("\x04"), // TODO: \x04?
			Int(3),
		},
		String(encodedEngineData),
		Sequence{
			String(""),
			String(""),
			newGetRequest(reqId, []Varbind{}),
		},
	}.Encode()

	if err != nil {
		return err
	}

	var decoded DataType
	var ok bool

	// TODO: make num of retries a package-level variable
	// TODO: turn this into a function?
	for i := 0; i < 3; i++ {
		c := make(chan DataType)
		s.sendRequest(discoverySequence, int(reqId), c)

		decoded, ok = <-c
		if ok {
			break
		} else {
			if i == 2 {
				return errors.New("discovery failed")
			}
		}
	}

	decodedEngineData, _, err := decode(bytes.NewReader([]byte(decoded.(Sequence)[2].(String))))
	if err != nil {
		return err
	}

	s.engineID = []byte(decodedEngineData.(Sequence)[0].(String))
	s.engineBoots = int32(decodedEngineData.(Sequence)[1].(Int))
	s.engineTime = int32(decodedEngineData.(Sequence)[2].(Int))

	s.privKey = passphraseToKey(s.privPassphrase, s.engineID)[:16]
	s.authKey = passphraseToKey(s.authPassphrase, s.engineID)

	return nil
}

func (s *Session) Get(oid ObjectIdentifier) (*GetResponse, error) {
	reqId := int(rand.Int31())

	getReq, err := Sequence{
		String(s.engineID),
		String(""),
		newGetRequest(reqId, []Varbind{NewVarbind(oid, Null)}),
	}.Encode()

	if err != nil {
		return nil, err
	}

	// TODO: turn this all into a function
	encrypted, priv := s.encrypt(getReq)

	packet, err := s.constructPacket(encrypted, priv)
	if err != nil {
		return nil, err
	}

	result, err := s.doRequest(packet, reqId)
	if err != nil {
		return nil, err
	}

	getRes, ok := result.(GetResponse)
	if !ok {
		return nil, ErrDecodingType
	}

	if len(getRes.varbinds) > 0 && getRes.varbinds[0].value == NoSuchInstance {
		return nil, errors.New("No Such Instance")
	}

	return &getRes, nil
}

func (s *Session) GetNext(oid ObjectIdentifier) (*GetResponse, error) {
	reqId := int(rand.Int31())

	getNextReq, err := Sequence{
		String(s.engineID),
		String(""),
		newGetNextRequest(reqId, []Varbind{NewVarbind(oid, Null)}),
	}.Encode()

	if err != nil {
		return nil, err
	}

	encrypted, priv := s.encrypt(getNextReq)

	packet, err := s.constructPacket(encrypted, priv)
	if err != nil {
		return nil, err
	}

	result, err := s.doRequest(packet, reqId)
	if err != nil {
		return nil, err
	}

	getRes, ok := result.(GetResponse)
	if !ok {
		return nil, ErrDecodingType
	}

	if len(getRes.varbinds) > 0 && getRes.varbinds[0].value == EndOfMIBView {
		return nil, errors.New("End of MIB View")
	}

	return &getRes, nil
}

func (s *Session) constructPacket(encrypted, priv []byte) ([]byte, error) {
	msgId := Int(rand.Int31())

	v3Header, err := Sequence{
		String(s.engineID),
		Int(s.engineBoots),
		Int(s.engineTime),
		String(s.user),
		String(strings.Repeat("\x00", 12)),
		String(priv),
	}.Encode()

	if err != nil {
		return nil, err
	}

	packet, err := Sequence{
		Int(3),
		Sequence{
			msgId,
			Int(65507),
			String("\x07"),
			Int(3),
		},
		String(v3Header),
		String(encrypted),
	}.Encode()

	if err != nil {
		return nil, err
	}

	authParam := s.auth(packet)

	return bytes.Replace(packet, bytes.Repeat([]byte{0}, 12), authParam, 1), nil
}
<|endoftext|>


<commit_before>/*
 * This file is part of the KubeVirt project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Copyright 2019 Red Hat, Inc.
 *
 */
package components

import (
	"fmt"

	secv1 "github.com/openshift/api/security/v1"
	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func GetAllSCC(namespace string) []*secv1.SecurityContextConstraints {
	return []*secv1.SecurityContextConstraints{
		NewKubeVirtHandlerSCC(namespace),
		NewKubeVirtControllerSCC(namespace),
	}
}

func newBlankSCC() *secv1.SecurityContextConstraints {
	return &secv1.SecurityContextConstraints{
		TypeMeta: metav1.TypeMeta{
			APIVersion: "security.openshift.io/v1",
			Kind:       "SecurityContextConstraints",
		},
	}
}

func NewKubeVirtHandlerSCC(namespace string) *secv1.SecurityContextConstraints {
	scc := newBlankSCC()

	scc.Name = "kubevirt-handler"
	scc.AllowPrivilegedContainer = true
	scc.AllowHostPID = true
	scc.AllowHostPorts = true
	scc.AllowHostIPC = true
	scc.RunAsUser = secv1.RunAsUserStrategyOptions{
		Type: secv1.RunAsUserStrategyRunAsAny,
	}
	scc.SELinuxContext = secv1.SELinuxContextStrategyOptions{
		Type: secv1.SELinuxStrategyRunAsAny,
	}
	scc.Volumes = []secv1.FSType{secv1.FSTypeAll}
	scc.AllowHostDirVolumePlugin = true
	scc.Users = []string{fmt.Sprintf("system:serviceaccount:%s:kubevirt-handler", namespace)}

	return scc
}

func NewKubeVirtControllerSCC(namespace string) *secv1.SecurityContextConstraints {
	scc := newBlankSCC()

	scc.Name = "kubevirt-controller"
	scc.AllowPrivilegedContainer = false
	scc.RunAsUser = secv1.RunAsUserStrategyOptions{
		Type: secv1.RunAsUserStrategyRunAsAny,
	}
	scc.SELinuxContext = secv1.SELinuxContextStrategyOptions{
		Type: secv1.SELinuxStrategyRunAsAny,
	}
	scc.AllowedCapabilities = []corev1.Capability{
		// add CAP_SYS_NICE capability to allow setting cpu affinity
		"SYS_NICE",
		// add CAP_NET_BIND_SERVICE capability to allow dhcp and slirp operations
		"NET_BIND_SERVICE",
		// add CAP_SYS_PTRACE capability needed for libvirt <8.1.0 to find the pid of swtpm
		"SYS_PTRACE",
	}
	scc.AllowHostDirVolumePlugin = true
	scc.Users = []string{fmt.Sprintf("system:serviceaccount:%s:kubevirt-controller", namespace)}

	return scc
}
<commit_msg>Add restricted seccomp profile to controller SCC<commit_after>/*
 * This file is part of the KubeVirt project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Copyright 2019 Red Hat, Inc.
 *
 */
package components

import (
	"fmt"

	secv1 "github.com/openshift/api/security/v1"
	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func GetAllSCC(namespace string) []*secv1.SecurityContextConstraints {
	return []*secv1.SecurityContextConstraints{
		NewKubeVirtHandlerSCC(namespace),
		NewKubeVirtControllerSCC(namespace),
	}
}

func newBlankSCC() *secv1.SecurityContextConstraints {
	return &secv1.SecurityContextConstraints{
		TypeMeta: metav1.TypeMeta{
			APIVersion: "security.openshift.io/v1",
			Kind:       "SecurityContextConstraints",
		},
	}
}

func NewKubeVirtHandlerSCC(namespace string) *secv1.SecurityContextConstraints {
	scc := newBlankSCC()

	scc.Name = "kubevirt-handler"
	scc.AllowPrivilegedContainer = true
	scc.AllowHostPID = true
	scc.AllowHostPorts = true
	scc.AllowHostIPC = true
	scc.RunAsUser = secv1.RunAsUserStrategyOptions{
		Type: secv1.RunAsUserStrategyRunAsAny,
	}
	scc.SELinuxContext = secv1.SELinuxContextStrategyOptions{
		Type: secv1.SELinuxStrategyRunAsAny,
	}
	scc.Volumes = []secv1.FSType{secv1.FSTypeAll}
	scc.AllowHostDirVolumePlugin = true
	scc.Users = []string{fmt.Sprintf("system:serviceaccount:%s:kubevirt-handler", namespace)}

	return scc
}

func NewKubeVirtControllerSCC(namespace string) *secv1.SecurityContextConstraints {
	scc := newBlankSCC()

	scc.Name = "kubevirt-controller"
	scc.AllowPrivilegedContainer = false
	scc.RunAsUser = secv1.RunAsUserStrategyOptions{
		Type: secv1.RunAsUserStrategyRunAsAny,
	}
	scc.SELinuxContext = secv1.SELinuxContextStrategyOptions{
		Type: secv1.SELinuxStrategyRunAsAny,
	}
	scc.SeccompProfiles = []string{
		"runtime/default",
	}
	scc.AllowedCapabilities = []corev1.Capability{
		// add CAP_SYS_NICE capability to allow setting cpu affinity
		"SYS_NICE",
		// add CAP_NET_BIND_SERVICE capability to allow dhcp and slirp operations
		"NET_BIND_SERVICE",
		// add CAP_SYS_PTRACE capability needed for libvirt <8.1.0 to find the pid of swtpm
		"SYS_PTRACE",
	}
	scc.AllowHostDirVolumePlugin = true
	scc.Users = []string{fmt.Sprintf("system:serviceaccount:%s:kubevirt-controller", namespace)}

	return scc
}
<|endoftext|>


<commit_before>package aws

import (
	"fmt"
	"log"
	"strings"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/lambda"
	"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
	"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func dataSourceAwsLambdaFunction() *schema.Resource {
	return &schema.Resource{
		Read: dataSourceAwsLambdaFunctionRead,

		Schema: map[string]*schema.Schema{
			"function_name": {
				Type:     schema.TypeString,
				Required: true,
			},
			"qualifier": {
				Type:     schema.TypeString,
				Optional: true,
			},
			"description": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"dead_letter_config": {
				Type:     schema.TypeList,
				Computed: true,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"target_arn": {
							Type:     schema.TypeString,
							Computed: true,
						},
					},
				},
			},
			"file_system_config": {
				Type:     schema.TypeList,
				Optional: true,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"arn": {
							Type:     schema.TypeString,
							Required: true,
						},
						"local_mount_path": {
							Type:     schema.TypeString,
							Required: true,
						},
					},
				},
			},
			"handler": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"layers": {
				Type:     schema.TypeList,
				Computed: true,
				Elem: &schema.Schema{
					Type: schema.TypeString,
				},
			},
			"memory_size": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"reserved_concurrent_executions": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"role": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"runtime": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"timeout": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"version": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"vpc_config": {
				Type:     schema.TypeList,
				Computed: true,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"subnet_ids": {
							Type:     schema.TypeSet,
							Computed: true,
							Elem:     &schema.Schema{Type: schema.TypeString},
							Set:      schema.HashString,
						},
						"security_group_ids": {
							Type:     schema.TypeSet,
							Computed: true,
							Elem:     &schema.Schema{Type: schema.TypeString},
							Set:      schema.HashString,
						},
						"vpc_id": {
							Type:     schema.TypeString,
							Computed: true,
						},
					},
				},
			},
			"arn": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"qualified_arn": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"invoke_arn": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"last_modified": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"source_code_hash": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"source_code_size": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"environment": {
				Type:     schema.TypeList,
				Computed: true,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"variables": {
							Type:     schema.TypeMap,
							Computed: true,
							Elem:     &schema.Schema{Type: schema.TypeString},
						},
					},
				},
			},
			"tracing_config": {
				Type:     schema.TypeList,
				Computed: true,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"mode": {
							Type:     schema.TypeString,
							Computed: true,
						},
					},
				},
			},
			"kms_key_arn": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"tags": tagsSchemaComputed(),
		},
	}
}

func dataSourceAwsLambdaFunctionRead(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).lambdaconn
	ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

	functionName := d.Get("function_name").(string)

	input := &lambda.GetFunctionInput{
		FunctionName: aws.String(functionName),
	}

	if v, ok := d.GetOk("qualifier"); ok {
		input.Qualifier = aws.String(v.(string))
	}

	log.Printf("[DEBUG] Getting Lambda Function: %s", input)
	output, err := conn.GetFunction(input)

	if err != nil {
		return fmt.Errorf("error getting Lambda Function (%s): %s", functionName, err)
	}

	if output == nil {
		return fmt.Errorf("error getting Lambda Function (%s): empty response", functionName)
	}

	function := output.Configuration

	functionARN := aws.StringValue(function.FunctionArn)
	qualifierSuffix := fmt.Sprintf(":%s", d.Get("qualifier").(string))
	versionSuffix := fmt.Sprintf(":%s", aws.StringValue(function.Version))

	qualifiedARN := functionARN
	if !strings.HasSuffix(functionARN, qualifierSuffix) && !strings.HasSuffix(functionARN, versionSuffix) {
		qualifiedARN = functionARN + versionSuffix
	}

	unqualifiedARN := strings.TrimSuffix(functionARN, qualifierSuffix)

	d.Set("arn", unqualifiedARN)

	deadLetterConfig := []interface{}{}
	if function.DeadLetterConfig != nil {
		deadLetterConfig = []interface{}{
			map[string]interface{}{
				"target_arn": aws.StringValue(function.DeadLetterConfig.TargetArn),
			},
		}
	}
	if err := d.Set("dead_letter_config", deadLetterConfig); err != nil {
		return fmt.Errorf("error setting dead_letter_config: %s", err)
	}

	d.Set("description", function.Description)

	if err := d.Set("environment", flattenLambdaEnvironment(function.Environment)); err != nil {
		return fmt.Errorf("error setting environment: %s", err)
	}

	d.Set("handler", function.Handler)
	d.Set("invoke_arn", lambdaFunctionInvokeArn(aws.StringValue(function.FunctionArn), meta))
	d.Set("kms_key_arn", function.KMSKeyArn)
	d.Set("last_modified", function.LastModified)

	if err := d.Set("layers", flattenLambdaLayers(function.Layers)); err != nil {
		return fmt.Errorf("Error setting layers for Lambda Function (%s): %s", d.Id(), err)
	}

	d.Set("memory_size", function.MemorySize)
	d.Set("qualified_arn", qualifiedARN)

	reservedConcurrentExecutions := int64(-1)
	if output.Concurrency != nil {
		reservedConcurrentExecutions = aws.Int64Value(output.Concurrency.ReservedConcurrentExecutions)
	}
	d.Set("reserved_concurrent_executions", reservedConcurrentExecutions)

	d.Set("role", function.Role)
	d.Set("runtime", function.Runtime)
	d.Set("source_code_hash", function.CodeSha256)
	d.Set("source_code_size", function.CodeSize)

	if err := d.Set("tags", keyvaluetags.LambdaKeyValueTags(output.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
		return fmt.Errorf("error setting tags: %s", err)
	}

	tracingConfig := []map[string]interface{}{
		{
			"mode": lambda.TracingModePassThrough,
		},
	}
	if function.TracingConfig != nil {
		tracingConfig[0]["mode"] = aws.StringValue(function.TracingConfig.Mode)
	}
	if err := d.Set("tracing_config", tracingConfig); err != nil {
		return fmt.Errorf("error setting tracing_config: %s", tracingConfig)
	}

	d.Set("timeout", function.Timeout)
	d.Set("version", function.Version)

	if err := d.Set("vpc_config", flattenLambdaVpcConfigResponse(function.VpcConfig)); err != nil {
		return fmt.Errorf("error setting vpc_config: %s", err)
	}

	if err := d.Set("file_system_config", flattenLambdaFileSystemConfigs(function.FileSystemConfigs)); err != nil {
		return fmt.Errorf("error setting file_system_config: %s", err)
	}

	d.SetId(aws.StringValue(function.FunctionName))

	return nil
}
<commit_msg>change attributes to computed<commit_after>package aws

import (
	"fmt"
	"log"
	"strings"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/lambda"
	"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
	"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func dataSourceAwsLambdaFunction() *schema.Resource {
	return &schema.Resource{
		Read: dataSourceAwsLambdaFunctionRead,

		Schema: map[string]*schema.Schema{
			"function_name": {
				Type:     schema.TypeString,
				Required: true,
			},
			"qualifier": {
				Type:     schema.TypeString,
				Optional: true,
			},
			"description": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"dead_letter_config": {
				Type:     schema.TypeList,
				Computed: true,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"target_arn": {
							Type:     schema.TypeString,
							Computed: true,
						},
					},
				},
			},
			"file_system_config": {
				Type:     schema.TypeList,
				Computed: true,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"arn": {
							Type:     schema.TypeString,
							Computed: true,
						},
						"local_mount_path": {
							Type:     schema.TypeString,
							Computed: true,
						},
					},
				},
			},
			"handler": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"layers": {
				Type:     schema.TypeList,
				Computed: true,
				Elem: &schema.Schema{
					Type: schema.TypeString,
				},
			},
			"memory_size": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"reserved_concurrent_executions": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"role": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"runtime": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"timeout": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"version": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"vpc_config": {
				Type:     schema.TypeList,
				Computed: true,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"subnet_ids": {
							Type:     schema.TypeSet,
							Computed: true,
							Elem:     &schema.Schema{Type: schema.TypeString},
							Set:      schema.HashString,
						},
						"security_group_ids": {
							Type:     schema.TypeSet,
							Computed: true,
							Elem:     &schema.Schema{Type: schema.TypeString},
							Set:      schema.HashString,
						},
						"vpc_id": {
							Type:     schema.TypeString,
							Computed: true,
						},
					},
				},
			},
			"arn": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"qualified_arn": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"invoke_arn": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"last_modified": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"source_code_hash": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"source_code_size": {
				Type:     schema.TypeInt,
				Computed: true,
			},
			"environment": {
				Type:     schema.TypeList,
				Computed: true,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"variables": {
							Type:     schema.TypeMap,
							Computed: true,
							Elem:     &schema.Schema{Type: schema.TypeString},
						},
					},
				},
			},
			"tracing_config": {
				Type:     schema.TypeList,
				Computed: true,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"mode": {
							Type:     schema.TypeString,
							Computed: true,
						},
					},
				},
			},
			"kms_key_arn": {
				Type:     schema.TypeString,
				Computed: true,
			},
			"tags": tagsSchemaComputed(),
		},
	}
}

func dataSourceAwsLambdaFunctionRead(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).lambdaconn
	ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

	functionName := d.Get("function_name").(string)

	input := &lambda.GetFunctionInput{
		FunctionName: aws.String(functionName),
	}

	if v, ok := d.GetOk("qualifier"); ok {
		input.Qualifier = aws.String(v.(string))
	}

	log.Printf("[DEBUG] Getting Lambda Function: %s", input)
	output, err := conn.GetFunction(input)

	if err != nil {
		return fmt.Errorf("error getting Lambda Function (%s): %s", functionName, err)
	}

	if output == nil {
		return fmt.Errorf("error getting Lambda Function (%s): empty response", functionName)
	}

	function := output.Configuration

	functionARN := aws.StringValue(function.FunctionArn)
	qualifierSuffix := fmt.Sprintf(":%s", d.Get("qualifier").(string))
	versionSuffix := fmt.Sprintf(":%s", aws.StringValue(function.Version))

	qualifiedARN := functionARN
	if !strings.HasSuffix(functionARN, qualifierSuffix) && !strings.HasSuffix(functionARN, versionSuffix) {
		qualifiedARN = functionARN + versionSuffix
	}

	unqualifiedARN := strings.TrimSuffix(functionARN, qualifierSuffix)

	d.Set("arn", unqualifiedARN)

	deadLetterConfig := []interface{}{}
	if function.DeadLetterConfig != nil {
		deadLetterConfig = []interface{}{
			map[string]interface{}{
				"target_arn": aws.StringValue(function.DeadLetterConfig.TargetArn),
			},
		}
	}
	if err := d.Set("dead_letter_config", deadLetterConfig); err != nil {
		return fmt.Errorf("error setting dead_letter_config: %s", err)
	}

	d.Set("description", function.Description)

	if err := d.Set("environment", flattenLambdaEnvironment(function.Environment)); err != nil {
		return fmt.Errorf("error setting environment: %s", err)
	}

	d.Set("handler", function.Handler)
	d.Set("invoke_arn", lambdaFunctionInvokeArn(aws.StringValue(function.FunctionArn), meta))
	d.Set("kms_key_arn", function.KMSKeyArn)
	d.Set("last_modified", function.LastModified)

	if err := d.Set("layers", flattenLambdaLayers(function.Layers)); err != nil {
		return fmt.Errorf("Error setting layers for Lambda Function (%s): %s", d.Id(), err)
	}

	d.Set("memory_size", function.MemorySize)
	d.Set("qualified_arn", qualifiedARN)

	reservedConcurrentExecutions := int64(-1)
	if output.Concurrency != nil {
		reservedConcurrentExecutions = aws.Int64Value(output.Concurrency.ReservedConcurrentExecutions)
	}
	d.Set("reserved_concurrent_executions", reservedConcurrentExecutions)

	d.Set("role", function.Role)
	d.Set("runtime", function.Runtime)
	d.Set("source_code_hash", function.CodeSha256)
	d.Set("source_code_size", function.CodeSize)

	if err := d.Set("tags", keyvaluetags.LambdaKeyValueTags(output.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
		return fmt.Errorf("error setting tags: %s", err)
	}

	tracingConfig := []map[string]interface{}{
		{
			"mode": lambda.TracingModePassThrough,
		},
	}
	if function.TracingConfig != nil {
		tracingConfig[0]["mode"] = aws.StringValue(function.TracingConfig.Mode)
	}
	if err := d.Set("tracing_config", tracingConfig); err != nil {
		return fmt.Errorf("error setting tracing_config: %s", tracingConfig)
	}

	d.Set("timeout", function.Timeout)
	d.Set("version", function.Version)

	if err := d.Set("vpc_config", flattenLambdaVpcConfigResponse(function.VpcConfig)); err != nil {
		return fmt.Errorf("error setting vpc_config: %s", err)
	}

	if err := d.Set("file_system_config", flattenLambdaFileSystemConfigs(function.FileSystemConfigs)); err != nil {
		return fmt.Errorf("error setting file_system_config: %s", err)
	}

	d.SetId(aws.StringValue(function.FunctionName))

	return nil
}
<|endoftext|>


<commit_before>package command

import (
	"os"
)

type MountOptions struct {
	filer                       *string
	filerMountRootPath          *string
	dir                         *string
	dirAutoCreate               *bool
	dirListCacheLimit           *int64
	collection                  *string
	replication                 *string
	ttlSec                      *int
	chunkSizeLimitMB            *int
	cacheDir                    *string
	cacheSizeMB                 *int64
	dataCenter                  *string
	allowOthers                 *bool
	umaskString                 *string
	nonempty                    *bool
	outsideContainerClusterMode *bool
}

var (
	mountOptions    MountOptions
	mountCpuProfile *string
	mountMemProfile *string
)

func init() {
	cmdMount.Run = runMount // break init cycle
	mountOptions.filer = cmdMount.Flag.String("filer", "localhost:8888", "weed filer location")
	mountOptions.filerMountRootPath = cmdMount.Flag.String("filer.path", "/", "mount this remote path from filer server")
	mountOptions.dir = cmdMount.Flag.String("dir", ".", "mount weed filer to this directory")
	mountOptions.dirAutoCreate = cmdMount.Flag.Bool("dirAutoCreate", false, "auto create the directory to mount to")
	mountOptions.dirListCacheLimit = cmdMount.Flag.Int64("dirListCacheLimit", 1000000, "limit cache size to speed up directory long format listing")
	mountOptions.collection = cmdMount.Flag.String("collection", "", "collection to create the files")
	mountOptions.replication = cmdMount.Flag.String("replication", "", "replication(e.g. 000, 001) to create to files. If empty, let filer decide.")
	mountOptions.ttlSec = cmdMount.Flag.Int("ttl", 0, "file ttl in seconds")
	mountOptions.chunkSizeLimitMB = cmdMount.Flag.Int("chunkSizeLimitMB", 16, "local write buffer size, also chunk large files")
	mountOptions.cacheDir = cmdMount.Flag.String("cacheDir", os.TempDir(), "local cache directory for file chunks")
	mountOptions.cacheSizeMB = cmdMount.Flag.Int64("cacheCapacityMB", 1000, "local cache capacity in MB (0 will disable cache)")
	mountOptions.dataCenter = cmdMount.Flag.String("dataCenter", "", "prefer to write to the data center")
	mountOptions.allowOthers = cmdMount.Flag.Bool("allowOthers", true, "allows other users to access the file system")
	mountOptions.umaskString = cmdMount.Flag.String("umask", "022", "octal umask, e.g., 022, 0111")
	mountOptions.nonempty = cmdMount.Flag.Bool("nonempty", false, "allows the mounting over a non-empty directory")
	mountCpuProfile = cmdMount.Flag.String("cpuprofile", "", "cpu profile output file")
	mountMemProfile = cmdMount.Flag.String("memprofile", "", "memory profile output file")
	mountOptions.outsideContainerClusterMode = cmdMount.Flag.Bool("outsideContainerClusterMode", false, "allows other users to access the file system")
}

var cmdMount = &Command{
	UsageLine: "mount -filer=localhost:8888 -dir=/some/dir",
	Short:     "mount weed filer to a directory as file system in userspace(FUSE)",
	Long: `mount weed filer to userspace.

  Pre-requisites:
  1) have SeaweedFS master and volume servers running
  2) have a "weed filer" running
  These 2 requirements can be achieved with one command "weed server -filer=true"

  This uses github.com/seaweedfs/fuse, which enables writing FUSE file systems on
  Linux, and OS X.

  On OS X, it requires OSXFUSE (http://osxfuse.github.com/).

  If the SeaweedFS system runs in a container cluster, e.g. managed by kubernetes or docker compose,
  the volume servers are not accessible by their own ip addresses.
  In "outsideContainerClusterMode", the mount will use the filer ip address instead, assuming:
    * All volume server containers are accessible through the same hostname or IP address as the filer.
    * All volume server container ports are open external to the cluster.

  `,
}
<commit_msg>adjust help message<commit_after>package command

import (
	"os"
)

type MountOptions struct {
	filer                       *string
	filerMountRootPath          *string
	dir                         *string
	dirAutoCreate               *bool
	dirListCacheLimit           *int64
	collection                  *string
	replication                 *string
	ttlSec                      *int
	chunkSizeLimitMB            *int
	cacheDir                    *string
	cacheSizeMB                 *int64
	dataCenter                  *string
	allowOthers                 *bool
	umaskString                 *string
	nonempty                    *bool
	outsideContainerClusterMode *bool
}

var (
	mountOptions    MountOptions
	mountCpuProfile *string
	mountMemProfile *string
)

func init() {
	cmdMount.Run = runMount // break init cycle
	mountOptions.filer = cmdMount.Flag.String("filer", "localhost:8888", "weed filer location")
	mountOptions.filerMountRootPath = cmdMount.Flag.String("filer.path", "/", "mount this remote path from filer server")
	mountOptions.dir = cmdMount.Flag.String("dir", ".", "mount weed filer to this directory")
	mountOptions.dirAutoCreate = cmdMount.Flag.Bool("dirAutoCreate", false, "auto create the directory to mount to")
	mountOptions.dirListCacheLimit = cmdMount.Flag.Int64("dirListCacheLimit", 1000000, "limit cache size to speed up directory long format listing")
	mountOptions.collection = cmdMount.Flag.String("collection", "", "collection to create the files")
	mountOptions.replication = cmdMount.Flag.String("replication", "", "replication(e.g. 000, 001) to create to files. If empty, let filer decide.")
	mountOptions.ttlSec = cmdMount.Flag.Int("ttl", 0, "file ttl in seconds")
	mountOptions.chunkSizeLimitMB = cmdMount.Flag.Int("chunkSizeLimitMB", 16, "local write buffer size, also chunk large files")
	mountOptions.cacheDir = cmdMount.Flag.String("cacheDir", os.TempDir(), "local cache directory for file chunks and meta data")
	mountOptions.cacheSizeMB = cmdMount.Flag.Int64("cacheCapacityMB", 1000, "local file chunk cache capacity in MB (0 will disable cache)")
	mountOptions.dataCenter = cmdMount.Flag.String("dataCenter", "", "prefer to write to the data center")
	mountOptions.allowOthers = cmdMount.Flag.Bool("allowOthers", true, "allows other users to access the file system")
	mountOptions.umaskString = cmdMount.Flag.String("umask", "022", "octal umask, e.g., 022, 0111")
	mountOptions.nonempty = cmdMount.Flag.Bool("nonempty", false, "allows the mounting over a non-empty directory")
	mountCpuProfile = cmdMount.Flag.String("cpuprofile", "", "cpu profile output file")
	mountMemProfile = cmdMount.Flag.String("memprofile", "", "memory profile output file")
	mountOptions.outsideContainerClusterMode = cmdMount.Flag.Bool("outsideContainerClusterMode", false, "allows other users to access the file system")
}

var cmdMount = &Command{
	UsageLine: "mount -filer=localhost:8888 -dir=/some/dir",
	Short:     "mount weed filer to a directory as file system in userspace(FUSE)",
	Long: `mount weed filer to userspace.

  Pre-requisites:
  1) have SeaweedFS master and volume servers running
  2) have a "weed filer" running
  These 2 requirements can be achieved with one command "weed server -filer=true"

  This uses github.com/seaweedfs/fuse, which enables writing FUSE file systems on
  Linux, and OS X.

  On OS X, it requires OSXFUSE (http://osxfuse.github.com/).

  If the SeaweedFS system runs in a container cluster, e.g. managed by kubernetes or docker compose,
  the volume servers are not accessible by their own ip addresses.
  In "outsideContainerClusterMode", the mount will use the filer ip address instead, assuming:
    * All volume server containers are accessible through the same hostname or IP address as the filer.
    * All volume server container ports are open external to the cluster.

  `,
}
<|endoftext|>


<commit_before>package markdown

import (
	"fmt"
	"regexp"
)

func BoldText(haystack, needle string) string {
	output := haystack
	return regexp.MustCompile(`(?i)\b(`+regexp.QuoteMeta(needle)+`)`).ReplaceAllString(output, "**$1**")
}

func UrlToMarkdownLinkHostname(url string) string {
	rx := regexp.MustCompile(`(?i)^https?://([^/]+)(/[^/])`)
	m := rx.FindStringSubmatch(url)
	if len(m) > 1 {
		suffix := ""
		if len(m) > 2 {
			suffix = "..."
		}
		return fmt.Sprintf("[%v%v](%v)", m[1], suffix, url)
	}
	return url
}
<commit_msg>update text/markdown<commit_after>package markdown

import (
	"fmt"
	"regexp"
)

// BoldText bodifies the identified text. It looks for start of words
// using a word boundary and will arbirarily end to match words with
// different suffixes.
func BoldText(haystack, needle string) string {
	output := haystack
	return regexp.MustCompile(`(?i)\b(`+regexp.QuoteMeta(needle)+`)`).ReplaceAllString(output, "**$1**")
}

func UrlToMarkdownLinkHostname(url string) string {
	rx := regexp.MustCompile(`(?i)^https?://([^/]+)(/[^/])`)
	m := rx.FindStringSubmatch(url)
	if len(m) > 1 {
		suffix := ""
		if len(m) > 2 {
			suffix = "..."
		}
		return fmt.Sprintf("[%v%v](%v)", m[1], suffix, url)
	}
	return url
}
<|endoftext|>


<commit_before>package gcloud

import (
	"cloud.google.com/go/storage"
	"context"
	"fmt"
	"github.com/hashicorp/terraform/backend"
	"github.com/hashicorp/terraform/helper/pathorcontents"
	"github.com/hashicorp/terraform/helper/schema"
	googleContext "golang.org/x/net/context"
	"golang.org/x/oauth2"
	"golang.org/x/oauth2/google"
	"google.golang.org/api/option"
)

func New() backend.Backend {
	s := &schema.Backend{
		Schema: map[string]*schema.Schema{
			"bucket": {
				Type:        schema.TypeString,
				Required:    true,
				Description: "The name of the Google Cloud Storage bucket",
			},

			"state_dir": {
				Type:        schema.TypeString,
				Optional:    true,
				Description: "The directory where state files will be saved inside the bucket",
			},

			"credentials": {
				Type:        schema.TypeString,
				Optional:    true,
				Description: "Google Cloud JSON Account Key",
				Default:     "",
			},
		},
	}

	result := &Backend{Backend: s}
	result.Backend.ConfigureFunc = result.configure
	return result
}

type Backend struct {
	*schema.Backend

	storageClient  *storage.Client
	storageContext googleContext.Context

	bucketName string
	stateDir   string
}

func (b *Backend) configure(ctx context.Context) error {
	if b.storageClient != nil {
		return nil
	}

	storageOAuth2Scope := "https://www.googleapis.com/auth/devstorage.read_write"

	data := schema.FromContextBackendConfig(ctx)

	b.bucketName = data.Get("bucket").(string)
	b.stateDir = data.Get("state_dir").(string)
	b.storageContext = googleContext.Background()

	credentials := data.Get("credentials").(string)

	var tokenSource oauth2.TokenSource

	if credentials != "" {
		credentialsJson, _, err := pathorcontents.Read(data.Get("credentials").(string))
		if err != nil {
			return fmt.Errorf("Error loading credentials: %v", err)
		}

		jwtConfig, err := google.JWTConfigFromJSON([]byte(credentialsJson), storageOAuth2Scope)
		if err != nil {
			return fmt.Errorf("Failed to get Google OAuth2 token: %v", err)
		}

		tokenSource = jwtConfig.TokenSource(b.storageContext)
	} else {
		defaultTokenSource, err := google.DefaultTokenSource(b.storageContext, storageOAuth2Scope)
		if err != nil {
			return fmt.Errorf("Failed to get Google Application Default Credentials: %v", err)
		}

		tokenSource = defaultTokenSource
	}

	client, err := storage.NewClient(b.storageContext, option.WithTokenSource(tokenSource))
	if err != nil {
		return fmt.Errorf("Failed to create Google Storage client: %v", err)
	}

	b.storageClient = client

	return nil
}
<commit_msg>backend/remote-state/gcloud: Use package provided OAuth scope.<commit_after>package gcloud

import (
	"cloud.google.com/go/storage"
	"context"
	"fmt"
	"github.com/hashicorp/terraform/backend"
	"github.com/hashicorp/terraform/helper/pathorcontents"
	"github.com/hashicorp/terraform/helper/schema"
	googleContext "golang.org/x/net/context"
	"golang.org/x/oauth2"
	"golang.org/x/oauth2/google"
	"google.golang.org/api/option"
)

func New() backend.Backend {
	s := &schema.Backend{
		Schema: map[string]*schema.Schema{
			"bucket": {
				Type:        schema.TypeString,
				Required:    true,
				Description: "The name of the Google Cloud Storage bucket",
			},

			"state_dir": {
				Type:        schema.TypeString,
				Optional:    true,
				Description: "The directory where state files will be saved inside the bucket",
			},

			"credentials": {
				Type:        schema.TypeString,
				Optional:    true,
				Description: "Google Cloud JSON Account Key",
				Default:     "",
			},
		},
	}

	result := &Backend{Backend: s}
	result.Backend.ConfigureFunc = result.configure
	return result
}

type Backend struct {
	*schema.Backend

	storageClient  *storage.Client
	storageContext googleContext.Context

	bucketName string
	stateDir   string
}

func (b *Backend) configure(ctx context.Context) error {
	if b.storageClient != nil {
		return nil
	}

	data := schema.FromContextBackendConfig(ctx)

	b.bucketName = data.Get("bucket").(string)
	b.stateDir = data.Get("state_dir").(string)
	b.storageContext = googleContext.Background()

	credentials := data.Get("credentials").(string)

	var tokenSource oauth2.TokenSource

	if credentials != "" {
		credentialsJson, _, err := pathorcontents.Read(data.Get("credentials").(string))
		if err != nil {
			return fmt.Errorf("Error loading credentials: %v", err)
		}

		jwtConfig, err := google.JWTConfigFromJSON([]byte(credentialsJson), storage.ScopeReadWrite)
		if err != nil {
			return fmt.Errorf("Failed to get Google OAuth2 token: %v", err)
		}

		tokenSource = jwtConfig.TokenSource(b.storageContext)
	} else {
		defaultTokenSource, err := google.DefaultTokenSource(b.storageContext, storage.ScopeReadWrite)
		if err != nil {
			return fmt.Errorf("Failed to get Google Application Default Credentials: %v", err)
		}

		tokenSource = defaultTokenSource
	}

	client, err := storage.NewClient(b.storageContext, option.WithTokenSource(tokenSource))
	if err != nil {
		return fmt.Errorf("Failed to create Google Storage client: %v", err)
	}

	b.storageClient = client

	return nil
}
<|endoftext|>


<commit_before>package imageutil

import (
	"fmt"
	"image"
	"image/color"
	"strings"

	"golang.org/x/image/draw"
)

// AddBackgroundWhite adds a white background which is usable
// when the image has a transparent background.
func AddBackgroundWhite(imgSrc image.Image) image.Image {
	imgNew := image.NewRGBA(imgSrc.Bounds())
	draw.Draw(imgNew, imgNew.Bounds(),
		&image.Uniform{color.White}, image.Point{}, draw.Src)
	draw.Draw(imgNew, imgNew.Bounds(),
		imgSrc, imgSrc.Bounds().Min, draw.Over)
	return imgNew
}

// Resize scales an image to the provided size units. Use a 0
// to scale the aspect ratio.
func Resize(width, height uint, src image.Image, scale draw.Scaler) image.Image {
	if width == 0 && height != 0 {
		aspect := ImageAspect(src)
		width = uint(aspect * float64(height))
	} else if height == 0 && width != 0 {
		aspect := ImageAspect(src)
		height = uint(float64(width) / aspect)
	}
	rect := image.Rect(0, 0, int(width), int(height))
	dst := image.NewRGBA(rect)
	scale.Scale(dst, rect, src, src.Bounds(), draw.Over, nil)
	return dst
}

func ImageAspect(img image.Image) float64 {
	return Aspect(ImageWidthHeight(img))
}

func Aspect(width, height int) float64 {
	return float64(width) / float64(height)
}

func ImageWidthHeight(img image.Image) (int, int) {
	return img.Bounds().Max.X - img.Bounds().Min.X,
		img.Bounds().Max.Y - img.Bounds().Min.Y
}

// Scale will resize the image to thee provided rectangle using the
// provided interpolation function.
func Scale(src image.Image, rect image.Rectangle, scale draw.Scaler) image.Image {
	dst := image.NewRGBA(rect)
	scale.Scale(dst, rect, src, src.Bounds(), draw.Over, nil)
	return dst
}

func DefaultScaler() draw.Scaler {
	return draw.NearestNeighbor
}

func ParseScalar(rawInterpolation string) (draw.Scaler, error) {
	rawInterpolation = strings.ToLower(strings.TrimSpace(rawInterpolation))
	switch rawInterpolation {
	case "nearestneighbor":
		return draw.NearestNeighbor, nil
	case "approxbilinear":
		return draw.ApproxBiLinear, nil
	case "bilinear":
		return draw.BiLinear, nil
	case "catmullrom":
		return draw.CatmullRom, nil
	}
	return draw.NearestNeighbor, fmt.Errorf("Cannot parse Scalar [%s]", rawInterpolation)
}
<commit_msg>update imageutil.DefaultScaler()<commit_after>package imageutil

import (
	"fmt"
	"image"
	"image/color"
	"strings"

	"golang.org/x/image/draw"
)

// AddBackgroundWhite adds a white background which is usable
// when the image has a transparent background.
func AddBackgroundWhite(imgSrc image.Image) image.Image {
	imgNew := image.NewRGBA(imgSrc.Bounds())
	draw.Draw(imgNew, imgNew.Bounds(),
		&image.Uniform{color.White}, image.Point{}, draw.Src)
	draw.Draw(imgNew, imgNew.Bounds(),
		imgSrc, imgSrc.Bounds().Min, draw.Over)
	return imgNew
}

// Resize scales an image to the provided size units. Use a 0
// to scale the aspect ratio.
func Resize(width, height uint, src image.Image, scale draw.Scaler) image.Image {
	if width == 0 && height != 0 {
		aspect := ImageAspect(src)
		width = uint(aspect * float64(height))
	} else if height == 0 && width != 0 {
		aspect := ImageAspect(src)
		height = uint(float64(width) / aspect)
	}
	rect := image.Rect(0, 0, int(width), int(height))
	dst := image.NewRGBA(rect)
	scale.Scale(dst, rect, src, src.Bounds(), draw.Over, nil)
	return dst
}

func ImageAspect(img image.Image) float64 {
	return Aspect(ImageWidthHeight(img))
}

func Aspect(width, height int) float64 {
	return float64(width) / float64(height)
}

func ImageWidthHeight(img image.Image) (int, int) {
	return img.Bounds().Max.X - img.Bounds().Min.X,
		img.Bounds().Max.Y - img.Bounds().Min.Y
}

// Scale will resize the image to the provided rectangle using the
// provided interpolation function.
func Scale(src image.Image, rect image.Rectangle, scale draw.Scaler) image.Image {
	dst := image.NewRGBA(rect)
	scale.Scale(dst, rect, src, src.Bounds(), draw.Over, nil)
	return dst
}

// DefaultScaler returns a general best results interpolation
// algorithm. See more here https://blog.codinghorror.com/better-image-resizing/ .
func DefaultScaler() draw.Scaler {
	return draw.BiLinear
}

func ParseScalar(rawInterpolation string) (draw.Scaler, error) {
	rawInterpolation = strings.ToLower(strings.TrimSpace(rawInterpolation))
	switch rawInterpolation {
	case "nearestneighbor":
		return draw.NearestNeighbor, nil
	case "approxbilinear":
		return draw.ApproxBiLinear, nil
	case "bilinear":
		return draw.BiLinear, nil
	case "catmullrom":
		return draw.CatmullRom, nil
	}
	return draw.NearestNeighbor, fmt.Errorf("Cannot parse Scalar [%s]", rawInterpolation)
}
<|endoftext|>


<commit_before>package rpcd

import (
	"github.com/Symantec/Dominator/imageunpacker/unpacker"
	"github.com/Symantec/Dominator/lib/srpc"
	"io"
	"log"
	"sync"
)

type srpcType struct {
	unpacker      *unpacker.Unpacker
	logger        *log.Logger
	addDeviceLock sync.Mutex
}

type htmlWriter srpcType

func (hw *htmlWriter) WriteHtml(writer io.Writer) {
	hw.writeHtml(writer)
}

func Setup(unpackerObj *unpacker.Unpacker, logger *log.Logger) *htmlWriter {
	srpcObj := srpcType{
		unpacker: unpackerObj,
		logger:   logger}
	srpc.RegisterName("ImageUnpacker", &srpcObj)
	return (*htmlWriter)(&srpcObj)
}
<commit_msg>Switch imageunpacker/rpcd to lib/log/serverlogger package.<commit_after>package rpcd

import (
	"github.com/Symantec/Dominator/imageunpacker/unpacker"
	"github.com/Symantec/Dominator/lib/log"
	"github.com/Symantec/Dominator/lib/srpc"
	"io"
	"sync"
)

type srpcType struct {
	unpacker      *unpacker.Unpacker
	logger        log.Logger
	addDeviceLock sync.Mutex
}

type htmlWriter srpcType

func (hw *htmlWriter) WriteHtml(writer io.Writer) {
	hw.writeHtml(writer)
}

func Setup(unpackerObj *unpacker.Unpacker, logger log.Logger) *htmlWriter {
	srpcObj := srpcType{
		unpacker: unpackerObj,
		logger:   logger}
	srpc.RegisterName("ImageUnpacker", &srpcObj)
	return (*htmlWriter)(&srpcObj)
}
<|endoftext|>