Skip to content

Commit b51f816

Browse files
committed
working fargate credentials
1 parent cf9029f commit b51f816

File tree

6 files changed

+41
-13
lines changed

6 files changed

+41
-13
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,20 @@ export IMMUDB_S3_ENDPOINT="https://${IMMUDB_S3_BUCKET_NAME}.s3.${IMMUDB_S3_LOCAT
184184
./immudb
185185
```
186186

187+
If using Fargate, the credentials URL can be sourced automatically:
188+
189+
```bash
190+
export IMMUDB_S3_STORAGE=true
191+
export IMMUDB_S3_ROLE_ENABLED=true
192+
export IMMUDB_S3_FARGATE_CREDENTIALS=true
193+
export IMMUDB_S3_BUCKET_NAME=<BUCKET NAME>
194+
export IMMUDB_S3_LOCATION=<AWS S3 REGION>
195+
export IMMUDB_S3_PATH_PREFIX=testing-001
196+
export IMMUDB_S3_ENDPOINT="https://${IMMUDB_S3_BUCKET_NAME}.s3.${IMMUDB_S3_LOCATION}.amazonaws.com"
197+
198+
./immudb
199+
```
200+
187201
Optionally, you can specify the exact role immudb should be using with:
188202

189203
```bash

cmd/immudb/command/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (cl *Commandline) setupFlags(cmd *cobra.Command, options *server.Options) {
8989
cmd.Flags().String("s3-path-prefix", "", "s3 path prefix (multiple immudb instances can share the same bucket if they have different prefixes)")
9090
cmd.Flags().Bool("s3-external-identifier", false, "use the remote identifier if there is no local identifier")
9191
cmd.Flags().String("s3-instance-metadata-url", "http://169.254.169.254", "s3 instance metadata url")
92-
cmd.Flags().String("s3-fargate-credentials-url", "", "s3 fargate credentials url")
92+
cmd.Flags().String("s3-fargate-credentials", "false", "s3 fargate credentials true/false")
9393
cmd.Flags().Int("max-sessions", 100, "maximum number of simultaneously opened sessions")
9494
cmd.Flags().Duration("max-session-inactivity-time", 3*time.Minute, "max session inactivity time is a duration after which an active session is declared inactive by the server. A session is kept active if server is still receiving requests from client (keep-alive or other methods)")
9595
cmd.Flags().Duration("max-session-age-time", 0, "the current default value is infinity. max session age time is a duration after which session will be forcibly closed")

cmd/immudb/command/parse_options.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func parseOptions() (options *server.Options, err error) {
106106
s3PathPrefix := viper.GetString("s3-path-prefix")
107107
s3ExternalIdentifier := viper.GetBool("s3-external-identifier")
108108
s3MetadataURL := viper.GetString("s3-instance-metadata-url")
109+
s3FargateCredentials := viper.GetBool("s3-fargate-credentials")
109110

110111
remoteStorageOptions := server.DefaultRemoteStorageOptions().
111112
WithS3Storage(s3Storage).
@@ -118,7 +119,8 @@ func parseOptions() (options *server.Options, err error) {
118119
WithS3Location(s3Location).
119120
WithS3PathPrefix(s3PathPrefix).
120121
WithS3ExternalIdentifier(s3ExternalIdentifier).
121-
WithS3InstanceMetadataURL(s3MetadataURL)
122+
WithS3InstanceMetadataURL(s3MetadataURL).
123+
WithS3FargateCredentials(s3FargateCredentials)
122124

123125
sessionOptions := sessions.DefaultOptions().
124126
WithMaxSessions(viper.GetInt("max-sessions")).

embedded/remotestorage/s3/s3.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ type Storage struct {
5757
awsInstanceMetadataURL string
5858
awsCredsRefreshPeriod time.Duration
5959

60-
fargateCredentialsURL string
60+
fargateCredentials bool
6161
}
6262

6363
var (
@@ -101,7 +101,7 @@ func Open(
101101
location string,
102102
prefix string,
103103
awsInstanceMetadataURL string,
104-
fargateCredentialsURL string,
104+
fargateCredentials bool,
105105
) (remotestorage.Storage, error) {
106106

107107
// Endpoint must always end with '/'
@@ -140,7 +140,7 @@ func Open(
140140
},
141141
awsInstanceMetadataURL: awsInstanceMetadataURL,
142142
awsCredsRefreshPeriod: time.Minute,
143-
fargateCredentialsURL: fargateCredentialsURL,
143+
fargateCredentials: fargateCredentials,
144144
}
145145

146146
err := s3storage.getRoleCredentials()
@@ -811,9 +811,15 @@ func (s *Storage) getRoleCredentials() error {
811811
}
812812

813813
func (s *Storage) requestCredentials() (string, string, string, error) {
814-
if s.fargateCredentialsURL != "" {
814+
if s.fargateCredentials {
815815
// Use Fargate credentials
816-
fargateReq, err := http.NewRequest("GET", s.fargateCredentialsURL, nil)
816+
fargateCredentialsRelativeURI := os.Getenv("AWS_CONTAINER_CREDENTIALS_RELATIVE_URI")
817+
if fargateCredentialsRelativeURI == "" {
818+
return "", "", "", errors.New(fmt.Sprintf("environment variable %s is not set or empty", "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"))
819+
}
820+
fargateCredentialsURL := fmt.Sprintf("%s%s", "http://169.254.170.2", fargateCredentialsRelativeURI)
821+
822+
fargateReq, err := http.NewRequest("GET", fargateCredentialsURL, nil)
817823
if err != nil {
818824
return "", "", "", errors.New("cannot form fargate credentials request")
819825
}

pkg/server/options.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ type RemoteStorageOptions struct {
9898
S3PathPrefix string
9999
S3ExternalIdentifier bool
100100
S3InstanceMetadataURL string
101-
FargateCredentialsURL string
101+
FargateCredentials bool
102102
}
103103

104104
type ReplicationOptions struct {
@@ -371,7 +371,11 @@ func (o *Options) String() string {
371371
opts = append(opts, "S3 storage")
372372
if o.RemoteStorageOptions.S3RoleEnabled {
373373
opts = append(opts, rightPad(" role auth", o.RemoteStorageOptions.S3RoleEnabled))
374-
opts = append(opts, rightPad(" role name", o.RemoteStorageOptions.S3Role))
374+
if o.RemoteStorageOptions.FargateCredentials {
375+
opts = append(opts, rightPad(" fargate creds", o.RemoteStorageOptions.FargateCredentials))
376+
} else {
377+
opts = append(opts, rightPad(" role name", o.RemoteStorageOptions.S3Role))
378+
}
375379
}
376380
opts = append(opts, rightPad(" endpoint", o.RemoteStorageOptions.S3Endpoint))
377381
opts = append(opts, rightPad(" bucket name", o.RemoteStorageOptions.S3BucketName))
@@ -380,7 +384,9 @@ func (o *Options) String() string {
380384
}
381385
opts = append(opts, rightPad(" prefix", o.RemoteStorageOptions.S3PathPrefix))
382386
opts = append(opts, rightPad(" external id", o.RemoteStorageOptions.S3ExternalIdentifier))
383-
opts = append(opts, rightPad(" metadata url", o.RemoteStorageOptions.S3InstanceMetadataURL))
387+
if !o.RemoteStorageOptions.FargateCredentials {
388+
opts = append(opts, rightPad(" metadata url", o.RemoteStorageOptions.S3InstanceMetadataURL))
389+
}
384390
}
385391
if o.AdminPassword == auth.SysAdminPassword {
386392
opts = append(opts, "----------------------------------------")
@@ -600,8 +606,8 @@ func (opts *RemoteStorageOptions) WithS3InstanceMetadataURL(url string) *RemoteS
600606
return opts
601607
}
602608

603-
func (opts *RemoteStorageOptions) WithFargateCredentialsURL(fargateCredentialsURL string) *RemoteStorageOptions {
604-
opts.FargateCredentialsURL = fargateCredentialsURL
609+
func (opts *RemoteStorageOptions) WithS3FargateCredentials(fargateCredentials bool) *RemoteStorageOptions {
610+
opts.FargateCredentials = fargateCredentials
605611
return opts
606612
}
607613

pkg/server/remote_storage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (s *ImmuServer) createRemoteStorageInstance() (remotestorage.Storage, error
6060
s.Options.RemoteStorageOptions.S3Location,
6161
s.Options.RemoteStorageOptions.S3PathPrefix,
6262
s.Options.RemoteStorageOptions.S3InstanceMetadataURL,
63-
s.Options.RemoteStorageOptions.FargateCredentialsURL,
63+
s.Options.RemoteStorageOptions.FargateCredentials,
6464
)
6565
}
6666

0 commit comments

Comments
 (0)