示例#1
0
文件: validate.go 项目: podtools/pod
func cmdValidate(c *cli.Context) {

	processFlags(c)

	if fileLocation == "" {
		cli.ShowCommandHelp(c, "create")
		log.Fatal("You must specify a configuration file to use")
	}

	log.Infof("Config file %s", fileLocation)

	file, e := ioutil.ReadFile(fileLocation)
	if e != nil {
		fmt.Printf("File error: %v\n", e)
		os.Exit(1)
	}

	jsontype := providers.JSONObject{}
	json.Unmarshal(file, &jsontype)

	log.Info("*********************************************************************************")
	log.Info("** Loading configuration file " + fileLocation)
	log.Info("*********************************************************************************")

	log.Info("Configuration for environment " + jsontype.Provider.ProviderConfig.Aws.Vpc.Name)
	log.Info("Environment has " + strconv.Itoa(len(jsontype.Containers)) + " servers defined")

	//Validate Provider document using JSONSchema
	validated, validateErrors := vmproviders.ValidateDocument()
	if validated {
		log.Info("Successfully validated provider configuration")
	} else {
		log.Error("Error validating provider configuration %v", validateErrors)
	}

	homedir := os.Getenv("HOME")

	creds := credentials.NewSharedCredentials(homedir+"/.aws/credentials", profile)

	credValue, err := creds.Get()
	if err != nil {
		fmt.Printf("Credential token= %v\n", credValue)
		fmt.Println(err)
		os.Exit(1)
	}

	ec2client := vmproviders.GetEC2Client(creds, "us-east-1")

	validateSuccess, validateWarnings, validateErr := vmproviders.Validate(ec2client, &jsontype)
	if validateSuccess {
		log.Infof("Successfully validated environment %v", jsontype.Provider.ProviderConfig.Aws.Vpc.Name)
		for w := range validateWarnings {
			log.Warnf(validateWarnings[w])
		}
	} else {
		for e := range validateErr {
			log.Errorf(validateErr[e].Error())
		}
	}
}
示例#2
0
// Svc configures the DynamoDB service to use
func Svc(opts config.Options) *dynamodb.DynamoDB {
	awsConfig := &aws.Config{Region: aws.String(opts.Storage.AWS.Region)}

	// If a session was passed... (AWS Lambda does this)
	if opts.Storage.AWS.SessionToken != "" {
		os.Setenv("AWS_SESSION_TOKEN", opts.Storage.AWS.SessionToken)
	}

	// Look in a variety of places for AWS credentials. First, try the credentials file set by AWS CLI tool.
	// Note the empty string instructs to look under default file path (different based on OS).
	// This file can have multiple profiles and a default profile will be used unless otherwise configured.
	// See: https://godoc.org/github.com/aws/aws-sdk-go/aws/credentials#SharedCredentialsProvider
	creds := credentials.NewSharedCredentials("", opts.Storage.AWS.CredProfile)
	_, err := creds.Get()
	// If that failed, try environment variables.
	if err != nil {
		// The following are checked:
		// Access Key ID: AWS_ACCESS_KEY_ID or AWS_ACCESS_KEY
		// Secret Access Key: AWS_SECRET_ACCESS_KEY or AWS_SECRET_KEY
		creds = credentials.NewEnvCredentials()
	}

	// If credentials were passed via config, then use those. They will take priority over other methods.
	if opts.Storage.AWS.AccessKeyID != "" && opts.Storage.AWS.SecretAccessKey != "" {
		creds = credentials.NewStaticCredentials(opts.Storage.AWS.AccessKeyID, opts.Storage.AWS.SecretAccessKey, "")
	}
	awsConfig.Credentials = creds

	return dynamodb.New(session.New(awsConfig))
}
示例#3
0
func TestMain(m *testing.M) {
	flag.Parse()
	if !*integration {
		fmt.Fprintln(os.Stderr, "Skipping integration tests")
		os.Exit(0)
	}
	cfg = &aws.Config{
		Region:      aws.String("us-west-2"),
		Endpoint:    aws.String("http://localhost:8000"),
		Credentials: credentials.NewSharedCredentials("", *awsprofile),
	}
	sess = session.New(cfg)
	if *dynamodebug {
		sess.Config.LogLevel = aws.LogLevel(aws.LogDebug)
	}

	if err := loadUserFixtures(sess); err != nil {
		fmt.Fprintf(os.Stderr, "Error loading 'user' integration fixtures: %s", err)
		os.Exit(1)
	}
	if err := loadPostFixtures(sess); err != nil {
		fmt.Fprintf(os.Stderr, "Error loading 'post' integration fixtures: %s", err)
		os.Exit(1)
	}
	os.Exit(m.Run())
}
示例#4
0
// createCommandFactory create a command factory and wraps the command processor
func createCommandFactory(cx *cli.Context, commandFunc func(*cli.Context, *commandFactory) error) {
	awsRegion := cx.GlobalString("region")
	awsProfile := cx.GlobalString("profile")
	awsCredentials := cx.GlobalString("crendentials")

	if awsRegion == "" {
		usage(cx, "you need to specify the region or export the environment variable AWS_DEFAULT_REGION")
	}

	// step: create a default aws configuration
	cfg := &aws.Config{Region: &awsRegion}

	// step: are we specifying a aws profile, if so, we need to be using the $HOME/.aws/credentials file
	if awsProfile != "" {
		cfg.Credentials = credentials.NewSharedCredentials(awsCredentials, awsProfile)
	}

	// step: create a command line factory
	factory := &commandFactory{
		s3:  newS3Client(cfg),
		kms: newKmsClient(cfg),
	}

	if err := commandFunc(cx, factory); err != nil {
		usage(cx, err.Error())
	}
}
示例#5
0
文件: root.go 项目: aom/apex
// PreRun sets up global tasks used for most commands, some use PreRunNoop
// to remove this default behaviour.
func preRun(c *cobra.Command, args []string) error {
	if l, err := log.ParseLevel(logLevel); err == nil {
		log.SetLevel(l)
	}

	config := aws.NewConfig()

	if profile != "" {
		config = config.WithCredentials(credentials.NewSharedCredentials("", profile))
	}

	Session = session.New(config)

	Project = &project.Project{
		Log:  log.Log,
		Path: ".",
	}

	if dryRun {
		log.SetLevel(log.WarnLevel)
		Project.Service = dryrun.New(Session)
		Project.Concurrency = 1
	} else {
		Project.Service = lambda.New(Session)
	}

	if chdir != "" {
		if err := os.Chdir(chdir); err != nil {
			return err
		}
	}

	return Project.Open()
}
示例#6
0
文件: goad.go 项目: goadapp/goad
// Start a test
func (t *Test) Start() <-chan queue.RegionsAggData {
	awsConfig := aws.NewConfig().WithRegion(t.config.Regions[0])

	if t.config.AwsProfile != "" {
		creds := credentials.NewSharedCredentials("", t.config.AwsProfile)
		if _, err := creds.Get(); err != nil {
			log.Fatal(err)
		}
		awsConfig.WithCredentials(creds)
	}

	infra, err := infrastructure.New(t.config.Regions, awsConfig)
	if err != nil {
		log.Fatal(err)
	}

	t.invokeLambdas(awsConfig, infra.QueueURL())

	results := make(chan queue.RegionsAggData)

	go func() {
		for result := range queue.Aggregate(awsConfig, infra.QueueURL(), t.config.TotalRequests) {
			results <- result
		}
		infra.Clean()
		close(results)
	}()

	return results
}
示例#7
0
func (c *Config) Connect() interface{} {

	c.readConf()

	var client AWSClient

	awsConfig := new(aws.Config)

	if len(c.Profile) > 0 {
		awsConfig = &aws.Config{
			Credentials: credentials.NewSharedCredentials(c.Awsconf, fmt.Sprintf("profile %s", c.Profile)),
			Region:      aws.String(c.Region),
			MaxRetries:  aws.Int(3),
		}

	} else {
		// use instance role
		awsConfig = &aws.Config{
			Region: aws.String(c.Region),
		}

	}

	sess := session.New(awsConfig)

	client.ec2conn = ec2.New(sess)

	return &client

}
示例#8
0
文件: main.go 项目: kahing/goofys
// Mount the file system based on the supplied arguments, returning a
// fuse.MountedFileSystem that can be joined to wait for unmounting.
func mount(
	ctx context.Context,
	bucketName string,
	mountPoint string,
	flags *FlagStorage) (mfs *fuse.MountedFileSystem, err error) {

	awsConfig := &aws.Config{
		Region: &flags.Region,
		Logger: GetLogger("s3"),
		//LogLevel: aws.LogLevel(aws.LogDebug),
	}

	if len(flags.Profile) > 0 {
		awsConfig.Credentials = credentials.NewSharedCredentials("", flags.Profile)
	}

	if len(flags.Endpoint) > 0 {
		awsConfig.Endpoint = &flags.Endpoint
	}

	// deprecate flags.UsePathRequest
	if flags.UsePathRequest {
		log.Infoln("--use-path-request is deprecated, it's always on")
	}
	awsConfig.S3ForcePathStyle = aws.Bool(true)

	goofys := NewGoofys(bucketName, awsConfig, flags)
	if goofys == nil {
		err = fmt.Errorf("Mount: initialization failed")
		return
	}
	server := fuseutil.NewFileSystemServer(goofys)

	fuseLog := GetLogger("fuse")

	// Mount the file system.
	mountCfg := &fuse.MountConfig{
		FSName:                  bucketName,
		Options:                 flags.MountOptions,
		ErrorLogger:             GetStdLogger(NewLogger("fuse"), logrus.ErrorLevel),
		DisableWritebackCaching: true,
	}

	if flags.DebugFuse {
		fuseLog.Level = logrus.DebugLevel
		log.Level = logrus.DebugLevel
		mountCfg.DebugLogger = GetStdLogger(fuseLog, logrus.DebugLevel)
	}

	mfs, err = fuse.Mount(mountPoint, server, mountCfg)
	if err != nil {
		err = fmt.Errorf("Mount: %v", err)
		return
	}

	return
}
示例#9
0
// ProfileCredentials checks to see if specific profile is being asked to use
func (config SsmagentConfig) ProfileCredentials() (credsInConfig *credentials.Credentials, err error) {
	// the credentials file location and profile to load
	credsInConfig = credentials.NewSharedCredentials(config.Profile.Path, config.Profile.Name)
	_, err = credsInConfig.Get()
	if err != nil {
		return nil, err
	}
	fmt.Printf("Using AWS credentials configured under %v user profile \n", config.Profile.Name)
	return
}
示例#10
0
文件: main.go 项目: ae6rt/s3rw
func main() {
	home := os.Getenv("HOME")
	if home == "" {
		log.Fatalf("HOME environment variable not set")
	}
	credentialsFile := fmt.Sprintf("%s/.aws/credentials", home)
	config := aws.NewConfig().WithCredentials(credentials.NewSharedCredentials(credentialsFile, *profile)).WithRegion(*region).WithMaxRetries(3)

	svc := s3.New(config)

	switch *op {
	case "get":
		params := &s3.GetObjectInput{
			Bucket: aws.String(*bucketName),
			Key:    aws.String(*objectKey),
		}

		resp, err := svc.GetObject(params)
		if err != nil {
			log.Fatal(err)
		}

		data, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			log.Fatal(err)
		}

		if err := ioutil.WriteFile(*fileName, data, 0644); err != nil {
			log.Fatal(err)
		}
	case "put":
		data, err := ioutil.ReadFile(*fileName)
		if err != nil {
			log.Fatal(err)
		}
		params := &s3.PutObjectInput{
			Bucket:               aws.String(*bucketName),
			Key:                  aws.String(*objectKey),
			Body:                 bytes.NewReader(data),
			ServerSideEncryption: aws.String("AES256"),
		}
		if *publicRead {
			params.ACL = aws.String("public-read")
		}
		if *contentType != "" {
			params.ContentType = aws.String(*contentType)
		}
		if _, err = svc.PutObject(params); err != nil {
			log.Fatal(err.Error())
		}
	default:
		log.Fatalf("Operation not supported %s\n", *op)
	}
}
示例#11
0
文件: options.go 项目: gitu/S3Proxy
// LoadDefaultOptions is used to load in the default options into the globally
// available options struct. This is typically one of the first things called
// on start up. After this all options can be overridden
func LoadDefaultOptions() {
	Options.CacheDir = "/tmp/S3Proxy/"
	Options.BindAddress = ":9090"
	Options.ObjectCacheTTL = time.Duration(1 * time.Minute)
	Options.Region = "eu-west-1"
	Options.Bucket = "example-bucket"
	Options.TokenKey = "test_keys/sample_key"
	Options.TokenMethod = jwt.SigningMethodRS512
	Options.CookieMaxAge = 3600
	Options.AwsCredentials = credentials.NewSharedCredentials("", "default")
}
示例#12
0
func checkCreds() (err error) {
	creds := credentials.NewEnvCredentials()
	_, err = creds.Get()
	if err == nil {
		// If ENV credentials are present, I don't need to check shared credentials on fs
		return
	}
	creds = credentials.NewSharedCredentials("", "")
	_, err = creds.Get()
	return
}
示例#13
0
文件: util.go 项目: TheBigBear/cli53
func getService(debug bool, profile string) *route53.Route53 {
	config := aws.Config{}
	if profile != "" {
		config.Credentials = credentials.NewSharedCredentials("", profile)
	}
	// ensures throttled requests are retried
	config.MaxRetries = aws.Int(100)
	if debug {
		config.LogLevel = aws.LogLevel(aws.LogDebug)
	}
	return route53.New(session.New(), &config)
}
示例#14
0
func (c *CredentialConfig) rootCredentials() client.ConfigProvider {
	config := &aws.Config{
		Region: aws.String(c.Region),
	}
	if c.AccessKey != "" || c.SecretKey != "" {
		config.Credentials = credentials.NewStaticCredentials(c.AccessKey, c.SecretKey, c.Token)
	} else if c.Profile != "" || c.Filename != "" {
		config.Credentials = credentials.NewSharedCredentials(c.Filename, c.Profile)
	}

	return session.New(config)
}
示例#15
0
func main() {

	reportTable := map[string]float64{}
	constTable := map[string]string{}
	screds := credentials.NewSharedCredentials("/Users/ilyakravchenko/.aws/credentials", "myal")
	config := aws.Config{Region: aws.String("us-east-1"), Credentials: screds}
	sess := session.New(&config)
	if sess == nil {
		fmt.Println("problems with authorization")
	}
	svc := ec2.New(sess)
	params := &ec2.DescribeInstancesInput{}
	resp, err := svc.DescribeInstances(params)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	for index, _ := range resp.Reservations {
		for _, instance := range resp.Reservations[index].Instances {
			for _, tag := range instance.Tags {
				if *tag.Key == "Name" {
					extraInfo := *instance.InstanceType + "," + *instance.PrivateIpAddress //store extra info
					constTable[*tag.Value] = extraInfo
					_, ok := reportTable[*tag.Value]
					if !ok {
						reportTable[*tag.Value] = getPrice(*instance.InstanceType)

					} else {
						reportTable[*tag.Value] += getPrice(*instance.InstanceType)
					}
				}
			}
		}
	}
	for key, _ := range reportTable {
		_, ok := constTable[key]
		if ok {
			val := reportTable[key]
			constTable[key] = strconv.FormatFloat(val, 'f', 2, 64) + "," + constTable[key]
		}
	}
	table := tablewriter.NewWriter(os.Stdout)
	table.SetHeader([]string{"Type", "Price", "IP", "Name"})
	for k, v := range constTable {
		s := strings.Split(v, ",")
		s = []string{s[1], s[0], s[2]}

		table.Append(append(s, k))
	}
	table.Render()
}
示例#16
0
文件: aws.go 项目: dzlab/qbench
func NewS3(filename, profile, region string) *S3 {
	var creds *credentials.Credentials
	if _, err := os.Stat(filename); err == nil {
		log.Printf("Connecting to AWS using credentials from '%s'", filename)
		creds = credentials.NewSharedCredentials(filename, profile)
	} else {
		log.Printf("AWS credentials file '%s' dosen't exists, I will be using EC2 Role credentials", filename)
		sess := session.New()
		creds = credentials.NewCredentials(&ec2rolecreds.EC2RoleProvider{Client: ec2metadata.New(sess)})
	}
	sess := session.New(&aws.Config{Credentials: creds, Region: aws.String(region)})
	return &S3{svc: s3.New(sess)}
}
示例#17
0
// GetSessionWithProfile will return a new aws session with optional profile
func GetSessionWithProfile(profile string) (*session.Session, error) {
	config := aws.NewConfig()

	// Get and save profile
	if profile != "" {
		profileName = profile
		config = config.WithCredentials(credentials.NewSharedCredentials("", profileName))

		// Debug creds...
		// fmt.Println(config.Credentials.Get())
	}

	return getSessionWithConfig(config)
}
示例#18
0
文件: util.go 项目: barnybug/cli53
func getConfig(c *cli.Context) *aws.Config {
	debug := c.Bool("debug")
	profile := c.String("profile")
	config := aws.Config{}
	if profile != "" {
		config.Credentials = credentials.NewSharedCredentials("", profile)
	}
	// ensures throttled requests are retried
	config.MaxRetries = aws.Int(100)
	if debug {
		config.LogLevel = aws.LogLevel(aws.LogDebug)
	}
	return &config
}
示例#19
0
func AWSConfig(key, secret, token, file, profile string) (conf *aws.Config) {
	var creds *credentials.Credentials
	if file != "" {
		creds = credentials.NewSharedCredentials(file, profile)
	}
	if key != "" && secret != "" {
		creds = credentials.NewStaticCredentials(key, secret, token)
	}

	conf = &aws.Config{
		Credentials: creds,
	}

	return conf
}
示例#20
0
文件: main.go 项目: CNDonny/scope
func main() {
	accounts := []string{"default", "default2", "otherprofile"}

	// Spin off a worker for each account to retrieve that account's
	bucketCh := make(chan *Bucket, 5)
	var wg sync.WaitGroup
	for _, acc := range accounts {
		wg.Add(1)
		go func(acc string) {
			sess := session.New(&aws.Config{Credentials: credentials.NewSharedCredentials("", acc)})
			if err := getAccountBuckets(sess, bucketCh, acc); err != nil {
				fmt.Fprintf(os.Stderr, "failed to get account %s's bucket info, %v\n", acc, err)
			}
			wg.Done()
		}(acc)
	}
	// Spin off a goroutine which will wait until all account buckets have been collected and
	// added to the bucketCh. Close the bucketCh so the for range below will exit once all
	// bucket info is printed.
	go func() {
		wg.Wait()
		close(bucketCh)
	}()

	// Receive from the bucket channel printing the information for each bucket to the console
	// when the bucketCh channel is drained.
	buckets := []*Bucket{}
	for b := range bucketCh {
		buckets = append(buckets, b)
	}

	sortBuckets(buckets)
	for _, b := range buckets {
		if b.Error != nil {
			fmt.Printf("Bucket %s, owned by: %s, failed: %v\n", b.Name, b.Owner, b.Error)
			continue
		}

		encObjs := b.encryptedObjects()
		fmt.Printf("Bucket: %s, owned by: %s, total objects: %d, failed objects: %d, encrypted objects: %d\n",
			b.Name, b.Owner, len(b.Objects), len(b.ErrObjects), len(encObjs))
		if len(encObjs) > 0 {
			for _, encObj := range encObjs {
				fmt.Printf("\t%s %s:%s/%s\n", encObj.EncryptionType, b.Region, b.Name, encObj.Key)
			}
		}
	}
}
示例#21
0
文件: root.go 项目: gerred/apex
// PreRun sets up global tasks used for most commands, some use PreRunNoop
// to remove this default behaviour.
func preRun(c *cobra.Command, args []string) error {
	if l, err := log.ParseLevel(logLevel); err == nil {
		log.SetLevel(l)
	}

	// credential defaults
	config := aws.NewConfig()

	// explicit profile
	if profile != "" {
		config = config.WithCredentials(credentials.NewSharedCredentials("", profile))
	}

	// support region from ~/.aws/config for AWS_PROFILE
	if profile == "" {
		profile = os.Getenv("AWS_PROFILE")
	}

	// region from ~/.aws/config
	if region, _ := utils.GetRegion(profile); region != "" {
		config = config.WithRegion(region)
	}

	Session = session.New(config)

	Project = &project.Project{
		Log:  log.Log,
		Path: ".",
	}

	if dryRun {
		log.SetLevel(log.WarnLevel)
		Project.Service = dryrun.New(Session)
		Project.Concurrency = 1
	} else {
		Project.Service = lambda.New(Session)
	}

	if chdir != "" {
		if err := os.Chdir(chdir); err != nil {
			return err
		}
	}

	return Project.Open()
}
示例#22
0
func main() {
	kingpin.Version("0.0.1")
	kingpin.Parse()

	var manifests Manifests
	var creds *credentials.Credentials
	var config aws.Config
	var credentialFile string
	var err error

	credentialFile, err = CredentialFilePath(*file)
	if err != nil {
		log.Fatal(err)
	}

	if Exists(credentialFile) {
		creds = credentials.NewSharedCredentials(credentialFile, *profile)
	} else {
		accessKey := GetAccessKey()
		secretKey := GetSecretKey()
		creds = credentials.NewStaticCredentials(accessKey, secretKey, "")
	}

	config = AWSConfig(creds, *region)
	cli := s3.New(&config)

	listObjectOutput, err := S3ListObjectOutput(cli, *bucket)
	if err != nil {
		log.Fatal(err)
	}

	manifests = GetManifests(listObjectOutput)

	sort.Sort(sort.Reverse(manifests))

	if *all {
		OutputAll(manifests)
	}

	if *oldest {
		Oldest(manifests)
	}

	Nth(manifests, *num)
}
示例#23
0
func main() {
	ss := session.New(
		&aws.Config{
			Region:      aws.String("ap-northeast-1"),
			Credentials: credentials.NewSharedCredentials("", ""),
		})
	svc := kinesis.New(ss)

	resp, err := svc.PutRecord(&kinesis.PutRecordInput{
		StreamName:   aws.String("kaoriya-kinesis-test"),
		PartitionKey: aws.String("cmd/send"),
		Data:         []byte("foobar"),
	})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(resp)
}
示例#24
0
文件: main.go 项目: threadwaste/finto
func main() {
	flag.Parse()

	if *printver {
		fmt.Println("finto", finto.Version)
		os.Exit(0)
	}

	logdest, err := prepareLog(*logfile)
	if err != nil {
		panic(err)
	}
	defer logdest.Close()

	config, err := LoadConfig(*fintorc)
	if err != nil {
		panic(err)
	}

	// SharedCredentialsProvider defaults to file=~/.aws/credentials and
	// profile=default when provided zero-value strings
	rs := finto.NewRoleSet(sts.New(session.New(), &aws.Config{
		Credentials: credentials.NewSharedCredentials(
			config.Credentials.File,
			config.Credentials.Profile,
		),
	}))

	for alias, arn := range config.Roles {
		rs.SetRole(alias, arn)
	}

	context, err := finto.InitFintoContext(rs, config.DefaultRole)
	if err != nil {
		fmt.Println("warning: default role not set:", err)
	}

	router := finto.FintoRouter(&context)
	handler := handlers.LoggingHandler(logdest, router)
	err = http.ListenAndServe(fmt.Sprint(*addr, ":", *port), handler)
	if err != nil {
		panic(err)
	}
}
示例#25
0
func main() {
	client := route53.New(session.New(&aws.Config{
		Region:      aws.String("ap-northeast-1"),
		Credentials: credentials.NewSharedCredentials("", "default"),
	}))

	hzresp, err := client.ListHostedZonesByName(&route53.ListHostedZonesByNameInput{})
	if err != nil {
		panic(err)
	}
	HostedZoneId := hzresp.HostedZones[0].Id
	rrsresp, err := client.ListResourceRecordSets(&route53.ListResourceRecordSetsInput{HostedZoneId: HostedZoneId})
	if err != nil {
		panic(err)
	}
	for i := range rrsresp.ResourceRecordSets {
		fmt.Println(awsutil.StringValue(*rrsresp.ResourceRecordSets[i]))
	}
}
示例#26
0
func main() {
	fileNames, err := fileNamesFromDir(".")
	if err != nil {
		log.Fatalf("Error reading dir, error: %v", err)
	}

	sess := session.New(&aws.Config{
		Credentials: credentials.NewSharedCredentials("", os.Getenv("CREDENTIAL_PROFILE")),
		Region:      aws.String(os.Getenv("REGION")),
	})

	uploader := s3manager.NewUploader(sess)

	wg := sync.WaitGroup{}
	wg.Add(len(fileNames))

	for _, fileName := range fileNames {
		go func(fileName string) {
			file, err := os.Open(fileName)
			if err != nil {
				log.Fatalf("Failed to open file, error: %v", err)
			}

			result, err := uploader.Upload(&s3manager.UploadInput{
				Body:   file,
				Bucket: aws.String(os.Getenv("BUCKET_NAME")),
				Key:    aws.String(fileName),
			})

			if err != nil {
				log.Fatalf("Failed to upload, error: %v", err)
			}

			log.Println("Successfully uploaded to", result.Location)

			wg.Done()
		}(fileName)
	}

	wg.Wait()
}
示例#27
0
// Lists all profiles in the default ~/.aws/credentials directory
func listProfiles() []*aws.Config {
	// Make sure the config file exists
	config := os.Getenv("HOME") + "/.aws/credentials"

	if _, err := os.Stat(config); os.IsNotExist(err) {
		fmt.Println("No credentials file found at: %s", config)
		os.Exit(1)
	}

	file, _ := ini.LoadFile(config)
	configs := make([]*aws.Config, 0)

	for key, _ := range file {
		config := &aws.Config{
			Credentials: credentials.NewSharedCredentials("", key),
		}
		configs = append(configs, config)
	}

	return configs
}
示例#28
0
func (g *Group) Run(args []string) int {
	cli := iam.New(session.New(&aws.Config{
		Region:      aws.String(g.config.Region),
		Credentials: credentials.NewSharedCredentials("", g.config.Profile),
	}))

	if len(args) < 1 {
		// use default
		g.ListGroup(cli)
	} else {
		if args[0] == "--help" || args[0] == "-h" {
			fmt.Printf("%s\n", g.Help())
			return 1
		} else if args[0] == "list" {
			g.ListGroup(cli)
		} else {
			fmt.Printf("%s not sub command.\n", args[0])
			return 1
		}
	}

	return 0
}
示例#29
0
//
// getCredentials retrieves the AWS credentials and bootstraps the cliCommand
//
func (r *cliCommand) getCredentials() func(cx *cli.Context) error {
	return func(cx *cli.Context) error {
		// step: ensure we have a region
		if cx.GlobalString("region") == "" {
			fmt.Fprintf(os.Stderr, "[error] you have not specified the aws region the resources reside\n")
			os.Exit(1)
		}
		config := &aws.Config{
			Region: aws.String(cx.GlobalString("region")),
		}

		// step: are we using static credentials
		if cx.GlobalString("access-key") != "" || cx.GlobalString("secret-ket") != "" {
			if cx.GlobalString("secret-key") == "" {
				return fmt.Errorf("you have specified a access key with a secret key")
			}
			if cx.GlobalString("access-key") == "" {
				return fmt.Errorf("you have specified a secret key with a access key")
			}
			config.Credentials = credentials.NewStaticCredentials(cx.GlobalString("access-key"),
				cx.GlobalString("secret-key"),
				cx.GlobalString("session-token"))
		} else if cx.GlobalString("profile") != "" {
			config.Credentials = credentials.NewSharedCredentials(
				cx.GlobalString("credentials"),
				cx.GlobalString("profile"))

		}

		// step: create the clients
		r.s3Client = s3.New(session.New(config))
		r.kmsClient = kms.New(session.New(config))
		r.uploader = s3manager.NewUploader(session.New(config))

		return nil
	}
}
示例#30
0
func main() {

	type config struct {
		profiles []string
		region   string
		action   string
		query    string
		timeout  time.Duration
	}

	// Flags go in here
	c := &config{}

	flag.StringVar(&c.action, "action", "", "Action to perform (ami)")
	flag.StringVar(&c.action, "a", "", "Action to perform (ami)")
	flag.StringVar(&c.query, "query", "", "Query value (e.g. ami-1234)")
	flag.StringVar(&c.query, "q", "", "Query value (e.g. ami-1234)")
	flag.DurationVar(&c.timeout, "timeout", 5*time.Second, "Timeout e.g. 5s")
	flag.BoolVar(&verbose, "verbose", false, "Verbose logging?")
	flag.BoolVar(&verbose, "v", false, "Verbose logging?")
	flag.Parse()

	if c.action == "" && c.query == "" {
		flag.Usage()
		os.Exit(1)
	}

	// Log verbosity
	if !verbose {
		log.SetOutput(ioutil.Discard)
	}

	c.profiles = listProfiles()

	// If we find our thing earlier
	doneChan := make(chan bool, 1)
	failChan := make(chan bool, 1)
	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, os.Interrupt, os.Kill)

	// Notify all async when complete
	go func() {
		var done sync.WaitGroup
		done.Add(len(c.profiles))

		// Loop through all of the accounts, search for instance in parallel
		for _, k := range c.profiles {
			go func(key string) {
				log.Println("Searching account", key)
				config := &aws.Config{
					Credentials: credentials.NewSharedCredentials("", key),
				}
				sess := session.New(config)
				svc := ec2.New(sess)

				var r interface{}
				switch strings.ToLower(c.action) {
				case "ami":
					r = queryAmi(svc, c.query)
				default:
					log.Fatalf("Action '%s' is not a valid action", c.action)
				}

				if r != nil {
					v, err := json.Marshal(r)
					checkError(err)
					fmt.Printf("%s", v)
					doneChan <- true
				}
				done.Done()
			}(k)
		}
		done.Wait()
		failChan <- true
	}()

	// Wait up to timeout, or when first result comes back
	select {
	case <-sigChan:
		log.Fatalf("Interrupt")
	case <-time.After(c.timeout):
		log.Fatalf("Timeout waiting for result")
	case <-failChan:
		log.Fatalf("No results returned")
	case <-doneChan:
		os.Exit(0)
	}
}