-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
105 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package lambroll | ||
|
||
import ( | ||
"context" | ||
"text/template" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/sts" | ||
"github.com/google/go-jsonnet" | ||
"github.com/google/go-jsonnet/ast" | ||
) | ||
|
||
type CallerIdentity struct { | ||
data map[string]any | ||
Resolver func(ctx context.Context) (*sts.GetCallerIdentityOutput, error) | ||
} | ||
|
||
func newCallerIdentity(cfg aws.Config) *CallerIdentity { | ||
return &CallerIdentity{ | ||
Resolver: func(ctx context.Context) (*sts.GetCallerIdentityOutput, error) { | ||
return sts.NewFromConfig(cfg).GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{}) | ||
}, | ||
} | ||
} | ||
|
||
func (c *CallerIdentity) resolve(ctx context.Context) error { | ||
if c.data != nil { | ||
return nil | ||
} | ||
res, err := c.Resolver(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
c.data = map[string]any{ | ||
"Account": *res.Account, | ||
"Arn": *res.Arn, | ||
"UserId": *res.UserId, | ||
} | ||
return nil | ||
} | ||
|
||
func (c *CallerIdentity) Account(ctx context.Context) string { | ||
if err := c.resolve(ctx); err != nil { | ||
return "" | ||
} | ||
return c.data["Account"].(string) | ||
} | ||
|
||
func (c *CallerIdentity) JsonnetNativeFuncs(ctx context.Context) []*jsonnet.NativeFunction { | ||
return []*jsonnet.NativeFunction{ | ||
{ | ||
Name: "caller_identity", | ||
Params: []ast.Identifier{}, | ||
Func: func(params []any) (any, error) { | ||
if err := c.resolve(ctx); err != nil { | ||
return nil, err | ||
} | ||
return c.data, nil | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (c *CallerIdentity) FuncMap(ctx context.Context) template.FuncMap { | ||
return template.FuncMap{ | ||
"caller_identity": func() map[string]any { | ||
if err := c.resolve(ctx); err != nil { | ||
return nil | ||
} | ||
return c.data | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package lambroll_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/sts" | ||
"github.com/fujiwara/lambroll" | ||
) | ||
|
||
func TestCallerIdentity(t *testing.T) { | ||
c := lambroll.NewCallerIdentity(aws.Config{}) | ||
c.Resolver = func(_ context.Context) (*sts.GetCallerIdentityOutput, error) { | ||
return &sts.GetCallerIdentityOutput{ | ||
Account: aws.String("123456789012"), | ||
Arn: aws.String("arn:aws:iam::123456789012:user/test-user"), | ||
UserId: aws.String("AIXXXXXXXXX"), | ||
}, nil | ||
} | ||
ctx := context.Background() | ||
if c.Account(ctx) != "123456789012" { | ||
t.Errorf("unexpected account id: %s", c.Account(ctx)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters