Skip to content

Commit

Permalink
Add main file
Browse files Browse the repository at this point in the history
  • Loading branch information
louisruch committed Nov 21, 2024
1 parent a9fcf69 commit c9d7f45
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ plan.tfplan
# IntelliJ IDEA project files
.idea
*.iprg

.DS_Store
91 changes: 91 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main

import (
"context"
"crypto/rand"
"flag"
"fmt"
"io"
"os"

"github.com/google/uuid"
"github.com/hashicorp/boundary-plugin-aws/internal/credential"
"github.com/hashicorp/boundary-plugin-aws/plugin/service/storage"
"github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/storagebuckets"
pb "github.com/hashicorp/boundary/sdk/pbs/plugin"
"google.golang.org/protobuf/types/known/structpb"
)

const (
localFileName = "testFileForBondary"
localFileSize = 10
)

func main() {
region := flag.String("region", "us-east-1", "AWS Region")
bucket := flag.String("bucket", "test-bucket", "AWS Bucket")
arn := flag.String("arn", "", "AWS Role ARN")
access := flag.String("access", "", "AWS Access Key ID")
secret := flag.String("secret", "", "AWS Secret Access Key")
flag.Parse()

p := new(storage.StoragePlugin)
ctx := context.Background()

var reqSecrets *structpb.Struct
if *access != "" {
if *secret == "" {
panic("secret is required when accessId is provided")
}
var err error
reqSecrets, err = structpb.NewStruct(map[string]any{
credential.ConstAccessKeyId: *access,
credential.ConstSecretAccessKey: *secret,
})
if err != nil {
panic(err)
}
} else if *arn == "" {
panic("arn is required when accessId is not provided")
}

reqAttrs, err := structpb.NewStruct(map[string]any{
credential.ConstRegion: *region,
credential.ConstDisableCredentialRotation: true,
credential.ConstRoleArn: *arn,
})
if err != nil {
panic(err)
}

f, err := os.Create(localFileName)
if err != nil {
panic(err)
}
defer f.Close()
_, err = io.Copy(f, io.LimitReader(rand.Reader, localFileSize))
if err != nil {
panic(err)
}
defer os.Remove(localFileName)

// TODO: put loop here
objectKey := uuid.New().String()
putObjReq := &pb.PutObjectRequest{
Bucket: &storagebuckets.StorageBucket{
BucketName: *bucket,
Attributes: reqAttrs,
Secrets: reqSecrets,
},
Key: objectKey,
Path: localFileName,
}

_, err = p.PutObject(ctx, putObjReq)
if err != nil {
fmt.Println(err)
return
}

fmt.Println("Done")
}

0 comments on commit c9d7f45

Please sign in to comment.