func setServices() { sess, err := session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, }) if err != nil { log.Fatal(err) } cwlogs = cloudwatchlogs.New(sess) ec2meta = ec2metadata.New(sess) }
// Lists all encrypted objects owned by an account. The `accounts` string // contains a list of profiles to use. // // Usage: // listObjectsConcurrentlv 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) { defer wg.Done() sess, err := session.NewSessionWithOptions(session.Options{ Profile: acc, }) if err != nil { fmt.Fprintf(os.Stderr, "failed to create session for account, %s, %v\n", acc, err) return } if err = getAccountBuckets(sess, bucketCh, acc); err != nil { fmt.Fprintf(os.Stderr, "failed to get account %s's bucket info, %v\n", acc, err) } }(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) } } } }
func awsSession() *session.Session { if sess == nil { var err error sess, err = session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, }) if err != nil { log.Fatal("awsSession: couldn't create an AWS session", err) } } return sess }
func newAwsGenerator( datacentre, profile string) ( result *awsGeneratorType, err error) { sess, err := session.NewSessionWithOptions( session.Options{ Config: aws.Config{Region: aws.String(datacentre)}, Profile: profile, }) if err != nil { return } svc := ec2.New(sess) result = &awsGeneratorType{svc: svc} return }
func (ed *EC2Discovery) refresh() (tg *config.TargetGroup, err error) { t0 := time.Now() defer func() { ec2SDRefreshDuration.Observe(time.Since(t0).Seconds()) if err != nil { ec2SDRefreshFailuresCount.Inc() } }() sess, err := session.NewSessionWithOptions(session.Options{ Config: *ed.aws, Profile: ed.profile, }) if err != nil { return nil, fmt.Errorf("could not create aws session: %s", err) } ec2s := ec2.New(sess) tg = &config.TargetGroup{ Source: *ed.aws.Region, } if err = ec2s.DescribeInstancesPages(nil, func(p *ec2.DescribeInstancesOutput, lastPage bool) bool { for _, r := range p.Reservations { for _, inst := range r.Instances { if inst.PrivateIpAddress == nil { continue } labels := model.LabelSet{ ec2LabelInstanceID: model.LabelValue(*inst.InstanceId), } labels[ec2LabelPrivateIP] = model.LabelValue(*inst.PrivateIpAddress) addr := net.JoinHostPort(*inst.PrivateIpAddress, fmt.Sprintf("%d", ed.port)) labels[model.AddressLabel] = model.LabelValue(addr) if inst.PublicIpAddress != nil { labels[ec2LabelPublicIP] = model.LabelValue(*inst.PublicIpAddress) labels[ec2LabelPublicDNS] = model.LabelValue(*inst.PublicDnsName) } labels[ec2LabelAZ] = model.LabelValue(*inst.Placement.AvailabilityZone) labels[ec2LabelInstanceState] = model.LabelValue(*inst.State.Name) labels[ec2LabelInstanceType] = model.LabelValue(*inst.InstanceType) if inst.VpcId != nil { labels[ec2LabelVPCID] = model.LabelValue(*inst.VpcId) subnetsMap := make(map[string]struct{}) for _, eni := range inst.NetworkInterfaces { subnetsMap[*eni.SubnetId] = struct{}{} } subnets := []string{} for k := range subnetsMap { subnets = append(subnets, k) } labels[ec2LabelSubnetID] = model.LabelValue( subnetSeparator + strings.Join(subnets, subnetSeparator) + subnetSeparator) } for _, t := range inst.Tags { name := strutil.SanitizeLabelName(*t.Key) labels[ec2LabelTag+model.LabelName(name)] = model.LabelValue(*t.Value) } tg.Targets = append(tg.Targets, labels) } } return true }); err != nil { return nil, fmt.Errorf("could not describe instances: %s", err) } return tg, nil }