|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/rand" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "os" |
| 10 | + |
| 11 | + "github.com/google/uuid" |
| 12 | + "github.com/hashicorp/boundary-plugin-aws/internal/credential" |
| 13 | + "github.com/hashicorp/boundary-plugin-aws/plugin/service/storage" |
| 14 | + "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/storagebuckets" |
| 15 | + pb "github.com/hashicorp/boundary/sdk/pbs/plugin" |
| 16 | + "google.golang.org/protobuf/types/known/structpb" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + localFileName = "testFileForBondary" |
| 21 | + localFileSize = 10 |
| 22 | +) |
| 23 | + |
| 24 | +func main() { |
| 25 | + region := flag.String("region", "us-east-1", "AWS Region") |
| 26 | + bucket := flag.String("bucket", "test-bucket", "AWS Bucket") |
| 27 | + arn := flag.String("arn", "", "AWS Role ARN") |
| 28 | + access := flag.String("access", "", "AWS Access Key ID") |
| 29 | + secret := flag.String("secret", "", "AWS Secret Access Key") |
| 30 | + flag.Parse() |
| 31 | + |
| 32 | + p := new(storage.StoragePlugin) |
| 33 | + ctx := context.Background() |
| 34 | + |
| 35 | + var reqSecrets *structpb.Struct |
| 36 | + if *access != "" { |
| 37 | + if *secret == "" { |
| 38 | + panic("secret is required when accessId is provided") |
| 39 | + } |
| 40 | + var err error |
| 41 | + reqSecrets, err = structpb.NewStruct(map[string]any{ |
| 42 | + credential.ConstAccessKeyId: *access, |
| 43 | + credential.ConstSecretAccessKey: *secret, |
| 44 | + }) |
| 45 | + if err != nil { |
| 46 | + panic(err) |
| 47 | + } |
| 48 | + } else if *arn == "" { |
| 49 | + panic("arn is required when accessId is not provided") |
| 50 | + } |
| 51 | + |
| 52 | + reqAttrs, err := structpb.NewStruct(map[string]any{ |
| 53 | + credential.ConstRegion: *region, |
| 54 | + credential.ConstDisableCredentialRotation: true, |
| 55 | + credential.ConstRoleArn: *arn, |
| 56 | + }) |
| 57 | + if err != nil { |
| 58 | + panic(err) |
| 59 | + } |
| 60 | + |
| 61 | + f, err := os.Create(localFileName) |
| 62 | + if err != nil { |
| 63 | + panic(err) |
| 64 | + } |
| 65 | + defer f.Close() |
| 66 | + _, err = io.Copy(f, io.LimitReader(rand.Reader, localFileSize)) |
| 67 | + if err != nil { |
| 68 | + panic(err) |
| 69 | + } |
| 70 | + defer os.Remove(localFileName) |
| 71 | + |
| 72 | + // TODO: put loop here |
| 73 | + objectKey := uuid.New().String() |
| 74 | + putObjReq := &pb.PutObjectRequest{ |
| 75 | + Bucket: &storagebuckets.StorageBucket{ |
| 76 | + BucketName: *bucket, |
| 77 | + Attributes: reqAttrs, |
| 78 | + Secrets: reqSecrets, |
| 79 | + }, |
| 80 | + Key: objectKey, |
| 81 | + Path: localFileName, |
| 82 | + } |
| 83 | + |
| 84 | + _, err = p.PutObject(ctx, putObjReq) |
| 85 | + if err != nil { |
| 86 | + fmt.Println(err) |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + fmt.Println("Done") |
| 91 | +} |
0 commit comments