This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement provider arg option caching
- Loading branch information
Showing
86 changed files
with
706 additions
and
353 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Package cache stores provider information in DynamoDB so we | ||
// don't need to call slow external APIs, like AWS SSO, | ||
// every time a user is setting up an Access Rule or making an | ||
// Access Request. | ||
package cache | ||
|
||
import ( | ||
"github.com/common-fate/ddb" | ||
"github.com/common-fate/granted-approvals/pkg/storage/keys" | ||
) | ||
|
||
// ProviderOption is an argument option that we've cached | ||
// from an Access Provider in DynamoDB. | ||
type ProviderOption struct { | ||
Provider string `json:"provider" dynamodbav:"provider"` | ||
Arg string `json:"arg" dynamodbav:"arg"` | ||
Label string `json:"label" dynamodbav:"label"` | ||
Value string `json:"value" dynamodbav:"value"` | ||
} | ||
|
||
func (r *ProviderOption) DDBKeys() (ddb.Keys, error) { | ||
keys := ddb.Keys{ | ||
PK: keys.ProviderOption.PK1, | ||
SK: keys.ProviderOption.SK1(r.Provider, r.Arg, r.Value), | ||
} | ||
|
||
return keys, nil | ||
} |
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,37 @@ | ||
package storage | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue" | ||
"github.com/aws/aws-sdk-go-v2/service/dynamodb" | ||
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types" | ||
"github.com/common-fate/ddb" | ||
"github.com/common-fate/granted-approvals/pkg/cache" | ||
"github.com/common-fate/granted-approvals/pkg/storage/keys" | ||
) | ||
|
||
type GetProviderOptions struct { | ||
ProviderID string | ||
ArgID string | ||
Result []cache.ProviderOption | ||
} | ||
|
||
func (q *GetProviderOptions) BuildQuery() (*dynamodb.QueryInput, error) { | ||
qi := dynamodb.QueryInput{ | ||
KeyConditionExpression: aws.String("PK = :pk1 and begins_with(SK, :sk1)"), | ||
ExpressionAttributeValues: map[string]types.AttributeValue{ | ||
":pk1": &types.AttributeValueMemberS{Value: keys.ProviderOption.PK1}, | ||
":sk1": &types.AttributeValueMemberS{Value: q.ProviderID + "#" + q.ArgID}, | ||
}, | ||
} | ||
return &qi, nil | ||
} | ||
|
||
func (q *GetProviderOptions) UnmarshalQueryOutput(out *dynamodb.QueryOutput) error { | ||
if len(out.Items) == 0 { | ||
return ddb.ErrNoItems | ||
} | ||
|
||
return attributevalue.UnmarshalListOfMaps(out.Items, &q.Result) | ||
|
||
} |
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,36 @@ | ||
package storage | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/common-fate/ddb" | ||
"github.com/common-fate/ddb/ddbtest" | ||
"github.com/common-fate/granted-approvals/pkg/cache" | ||
) | ||
|
||
func TestGetProviderOptions(t *testing.T) { | ||
db := newTestingStorage(t) | ||
|
||
po := cache.ProviderOption{ | ||
Provider: "test", | ||
Arg: "test", | ||
Label: "test", | ||
Value: "test", | ||
} | ||
ddbtest.PutFixtures(t, db, &po) | ||
|
||
tc := []ddbtest.QueryTestCase{ | ||
{ | ||
Name: "ok", | ||
Query: &GetProviderOptions{ProviderID: "test", ArgID: "test"}, | ||
Want: &GetProviderOptions{ProviderID: "test", ArgID: "test", Result: []cache.ProviderOption{po}}, | ||
}, | ||
{ | ||
Name: "not found", | ||
Query: &GetProviderOptions{ProviderID: "somethingelse", ArgID: "test"}, | ||
WantErr: ddb.ErrNoItems, | ||
}, | ||
} | ||
|
||
ddbtest.RunQueryTests(t, db, tc) | ||
} |
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,13 @@ | ||
package keys | ||
|
||
const ProviderOptionKey = "PROVIDER_OPTION#" | ||
|
||
type providerOptionKeys struct { | ||
PK1 string | ||
SK1 func(providerID, argID, value string) string | ||
} | ||
|
||
var ProviderOption = providerOptionKeys{ | ||
PK1: ProviderOptionKey, | ||
SK1: func(providerID, argID, value string) string { return providerID + "#" + argID + "#" + value }, | ||
} |
Oops, something went wrong.