Home > Software design >  Need help understanding golang code in opensource project
Need help understanding golang code in opensource project

Time:01-14

I use an open-source tool called AWS-nuke to delete resources from AWS. I wanted to understand if the tool is modifying the "logging enabled" feature of S3 buckets before deleting them.

I found this piece of code that, in fact, seems to be doing so and modifying the property in the bucket to be deleted but I am not very familiarized with golang syntax and I cannot understand what is it actually doing, I would appreciate an explanation (for someone who doesn't code in golang) of this small piece of code!

This is the full code: https://github.com/rebuy-de/aws-nuke/blob/f7337e5c069b424605f80eac24f117a6394fb410/resources/s3-buckets.go#L93

The part I am interested in is:

_, err = e.svc.PutBucketLogging(&s3.PutBucketLoggingInput{
        Bucket:              &e.name,
        BucketLoggingStatus: &s3.BucketLoggingStatus{},
    })
    if err != nil {
        return err
    }

and there is also this struct definition:

type S3Bucket struct {
    svc          *s3.S3
    name         string
    creationDate time.Time
    tags         []*s3.Tag
}

I can't understand what is &s3.BucketLoggingStatus{} and why (I think) this is disabling the logging status of the bucket before deleting it.

Thanks for reading and the help!

CodePudding user response:

As per this full code: https://github.com/rebuy-de/aws-nuke/blob/f7337e5c069b424605f80eac24f117a6394fb410/resources/s3-buckets.go#L93

It is trying to :

  1. remove all bucket policy
  2. disable logging
  3. remove versions and
  4. remove objects

before deleting the actual bucket. Now, the meaning of this code snippet is,

_, err = e.svc.PutBucketLogging(&s3.PutBucketLoggingInput{
        Bucket:              &e.name,
        BucketLoggingStatus: &s3.BucketLoggingStatus{},
    })
    if err != nil {
        return err
    }

disabling the Logging of buckets (becasue &s3.BucketLoggingStatus{} this is empty struct). Let's suppose you don't want to delete bucket and wanted to enable the logging. So you can do like,

type LoggingEnabled struct {
    TargetBucket *string `type:"string" required:"true"`
    TargetGrants []*TargetGrant `locationNameList:"Grant" type:"list"`
    TargetPrefix *string `type:"string" required:"true"`
}

_, err = e.svc.PutBucketLogging(&s3.PutBucketLoggingInput{
        Bucket:              &e.name,
        BucketLoggingStatus: &s3.BucketLoggingStatus{
               LoggingEnabled: &LoggingEnabled
 },
    })
    if err != nil {
        return err
    }
  • Related