-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add lambda to create certificate provider
- Loading branch information
Showing
19 changed files
with
550 additions
and
299 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package ddb | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/dynamodb" | ||
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" | ||
"github.com/aws/aws-xray-sdk-go/xray" | ||
"github.com/ministryofjustice/opg-data-lpa-store/internal/shared" | ||
) | ||
|
||
type Client struct { | ||
ddb *dynamodb.DynamoDB | ||
tableName string | ||
} | ||
|
||
func (c *Client) Put(ctx context.Context, data any) error { | ||
item, err := dynamodbattribute.MarshalMap(data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = c.ddb.PutItemWithContext(ctx, &dynamodb.PutItemInput{ | ||
TableName: aws.String(c.tableName), | ||
Item: item, | ||
}) | ||
|
||
return err | ||
} | ||
|
||
func (c *Client) Get(ctx context.Context, uid string) (shared.Lpa, error) { | ||
lpa := shared.Lpa{} | ||
|
||
marshalledUid, err := dynamodbattribute.Marshal(uid) | ||
if err != nil { | ||
return lpa, err | ||
} | ||
|
||
getItemOutput, err := c.ddb.GetItemWithContext(ctx, &dynamodb.GetItemInput{ | ||
TableName: aws.String(c.tableName), | ||
Key: map[string]*dynamodb.AttributeValue{ | ||
"uid": marshalledUid, | ||
}, | ||
}) | ||
|
||
if err != nil { | ||
return lpa, err | ||
} | ||
|
||
err = dynamodbattribute.UnmarshalMap(getItemOutput.Item, &lpa) | ||
|
||
return lpa, err | ||
} | ||
|
||
func New(endpoint, tableName string) *Client { | ||
sess := session.Must(session.NewSession()) | ||
sess.Config.Endpoint = &endpoint | ||
|
||
c := &Client{ | ||
ddb: dynamodb.New(sess), | ||
tableName: tableName, | ||
} | ||
|
||
xray.AWS(c.ddb.Client) | ||
|
||
return c | ||
} |
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 |
---|---|---|
@@ -1,71 +1 @@ | ||
package shared | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/dynamodb" | ||
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" | ||
"github.com/aws/aws-xray-sdk-go/xray" | ||
) | ||
|
||
type DynamoDBClient struct { | ||
ddb *dynamodb.DynamoDB | ||
tableName string | ||
} | ||
|
||
func (c DynamoDBClient) Put(ctx context.Context, data Lpa) error { | ||
item, err := dynamodbattribute.MarshalMap(data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = c.ddb.PutItemWithContext(ctx, &dynamodb.PutItemInput{ | ||
TableName: aws.String(c.tableName), | ||
Item: item, | ||
}) | ||
|
||
return err | ||
} | ||
|
||
func (c DynamoDBClient) Get(ctx context.Context, uid string) (Lpa, error) { | ||
lpa := Lpa{} | ||
|
||
marshalledUid, err := dynamodbattribute.Marshal(uid) | ||
if err != nil { | ||
return lpa, err | ||
} | ||
|
||
getItemOutput, err := c.ddb.GetItemWithContext(ctx, &dynamodb.GetItemInput{ | ||
TableName: aws.String(c.tableName), | ||
Key: map[string]*dynamodb.AttributeValue{ | ||
"uid": marshalledUid, | ||
}, | ||
}) | ||
|
||
if err != nil { | ||
return lpa, err | ||
} | ||
|
||
err = dynamodbattribute.UnmarshalMap(getItemOutput.Item, &lpa) | ||
|
||
return lpa, err | ||
} | ||
|
||
func NewDynamoDB(tableName string) DynamoDBClient { | ||
sess := session.Must(session.NewSession()) | ||
|
||
endpoint := os.Getenv("AWS_DYNAMODB_ENDPOINT") | ||
sess.Config.Endpoint = &endpoint | ||
|
||
c := DynamoDBClient{ | ||
ddb: dynamodb.New(sess), | ||
tableName: tableName, | ||
} | ||
|
||
xray.AWS(c.ddb.Client) | ||
|
||
return c | ||
} |
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,12 @@ | ||
package shared | ||
|
||
type Lang string | ||
|
||
var ( | ||
LangCy = Lang("cy") | ||
LangEn = Lang("en") | ||
) | ||
|
||
func (l Lang) IsValid() bool { | ||
return l == LangCy || l == LangEn | ||
} |
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,89 @@ | ||
package validate | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"time" | ||
|
||
"github.com/ministryofjustice/opg-data-lpa-store/internal/shared" | ||
) | ||
|
||
var countryCodeRe = regexp.MustCompile("^[A-Z]{2}$") | ||
|
||
func All(fieldErrors ...[]shared.FieldError) []shared.FieldError { | ||
var errors []shared.FieldError | ||
|
||
for _, e := range fieldErrors { | ||
if e != nil { | ||
errors = append(errors, e...) | ||
} | ||
} | ||
|
||
return errors | ||
} | ||
|
||
func IfElse(ok bool, eIf []shared.FieldError, eElse []shared.FieldError) []shared.FieldError { | ||
if ok { | ||
return eIf | ||
} | ||
|
||
return eElse | ||
} | ||
|
||
func If(ok bool, e []shared.FieldError) []shared.FieldError { | ||
return IfElse(ok, e, nil) | ||
} | ||
|
||
func Required(source string, value string) []shared.FieldError { | ||
return If(value == "", []shared.FieldError{{Source: source, Detail: "field is required"}}) | ||
} | ||
|
||
func Empty(source string, value string) []shared.FieldError { | ||
return If(value != "", []shared.FieldError{{Source: source, Detail: "field must not be provided"}}) | ||
} | ||
|
||
func Date(source string, date shared.Date) []shared.FieldError { | ||
if date.IsMalformed { | ||
return []shared.FieldError{{Source: source, Detail: "invalid format"}} | ||
} | ||
|
||
if date.IsZero() { | ||
return []shared.FieldError{{Source: source, Detail: "field is required"}} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func Time(source string, t time.Time) []shared.FieldError { | ||
return If(t.IsZero(), []shared.FieldError{{Source: source, Detail: "field is required"}}) | ||
} | ||
|
||
func Address(prefix string, address shared.Address) []shared.FieldError { | ||
return All( | ||
Required(fmt.Sprintf("%s/line1", prefix), address.Line1), | ||
Required(fmt.Sprintf("%s/town", prefix), address.Town), | ||
Required(fmt.Sprintf("%s/country", prefix), address.Country), | ||
If(!countryCodeRe.MatchString(address.Country), []shared.FieldError{{Source: fmt.Sprintf("%s/country", prefix), Detail: "must be a valid ISO-3166-1 country code"}}), | ||
) | ||
} | ||
|
||
type isValid interface { | ||
~string | ||
IsValid() bool | ||
} | ||
|
||
func IsValid[V isValid](source string, v V) []shared.FieldError { | ||
if e := Required(source, string(v)); e != nil { | ||
return e | ||
} | ||
|
||
if !v.IsValid() { | ||
return []shared.FieldError{{Source: source, Detail: "invalid value"}} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func Unset(source string, v interface{ Unset() bool }) []shared.FieldError { | ||
return If(!v.Unset(), []shared.FieldError{{Source: source, Detail: "field must not be provided"}}) | ||
} |
Oops, something went wrong.