Skip to content

Commit

Permalink
target tests passed
Browse files Browse the repository at this point in the history
  • Loading branch information
zLukas committed Oct 5, 2023
1 parent 6c90a68 commit 607b607
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 99 deletions.
35 changes: 24 additions & 11 deletions src/cert-generator/cmd/lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package cmd
import (
"context"
"fmt"
"os"
"time"

"github.com/aws/aws-lambda-go/lambda"
"github.com/zLukas/CloudTools/src/cert-generator/pkg/aws"
"github.com/zLukas/CloudTools/src/cert-generator/pkg/tls"
)

type RequestEvent struct {
CACert tls.CACert `json:"caCert"`
Cert tls.Cert `json:"cert"`
CACert tls.CACert `json:"caCert"`
Cert tls.Cert `json:"cert"`
Requester string `json:"requester"`
}

func handleRequest(ctx context.Context, event RequestEvent) (string, error) {
Expand All @@ -24,22 +27,32 @@ func handleRequest(ctx context.Context, event RequestEvent) (string, error) {
if err != nil {
return "fail", fmt.Errorf("Failed to create Cert: %s", err.Error())
}
//dbTable := os.Getenv("TABLE_NAME")
dbTable := "CertTable"
dbTable := os.Getenv("TABLE_NAME")
dbRegion := os.Getenv("DB_REGION")
db := aws.Database{}
if err != nil {
fmt.Printf("Error: %s", err)
}
currentTime := time.Now()

err = db.PutItem(aws.TableRecord{
CaCert: aws.CertItem{PrivateKey: caKey,
Cert: ca,
CaCert: aws.CertItem{
PrivateKey: caKey,
Cert: ca,
},
CeCert: aws.CertItem{PrivateKey: ceKey,
Cert: ce,
CeCert: aws.CertItem{
PrivateKey: ceKey,
Cert: ce,
},
Name: "sample-record",
CreationDate: "today",
Name: event.Requester,
CreationDate: currentTime.Format("2006.01.02 15:04:05"),
},
aws.WithDynamoDBLogin(),
aws.WithDynamoDBLogin(dbRegion),
aws.WithTableName(dbTable),
)
if err != nil {
fmt.Printf("database upload error: %s", err.Error())
}

return "sucess", nil

Expand Down
28 changes: 0 additions & 28 deletions src/cert-generator/cmd/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"os"

"github.com/zLukas/CloudTools/src/cert-generator/pkg/aws"
"github.com/zLukas/CloudTools/src/cert-generator/pkg/input"
"github.com/zLukas/CloudTools/src/cert-generator/pkg/tls"
)
Expand All @@ -27,33 +26,6 @@ func RunLocal() {
fmt.Printf("Error: %s", err)
}

for k, el := range config.Cfg.Cert {

ceKey, ce, err := tls.CreateCertBytes(el, caKey, ca)
if err != nil {
fmt.Printf("Error: %s", err)
}
tls.WriteKeyCertFile(ceKey, ce, k+".pem")
}

tls.WriteKeyCertFile(caKey, ca, "CA-Certificate.pem")

fmt.Print("uploading to database...\n")
dbTable := "Certificates"
db := aws.Database{}
err = db.PutItem(aws.TableRecord{
CaCert: aws.CertItem{
PrivateKey: caKey,
Cert: ca,
},
CeCert: aws.CertItem{},
Name: "sample-record",
CreationDate: "today",
},
aws.WithDynamoDBLogin(),
aws.WithTableName(dbTable),
)
if err != nil {
fmt.Printf("database upload error: %s", err.Error())
}
}
59 changes: 0 additions & 59 deletions src/cert-generator/pkg/aws/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,17 @@ package aws

import (
"fmt"
"log"

"context"

"github.com/aws/aws-sdk-go-v2/config"
"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/aws"
)

type CertItem struct {
PrivateKey []byte `dynamodbav:"privateKey"`
Cert []byte `dynamodbav:"cert"`
}

type TableRecord struct {
CaCert CertItem `dynamodbav:"ca"`
CeCert CertItem `dynamodbav:"ce"`
Name string `dynamodbav:"name"`
CreationDate string `dynamodbav:"creationDate"`
}
type Database struct {
TableName string
Client interface{}
}

type DatabaseOption func(*Database)

func WithDynamoDBLogin() DatabaseOption {
return func(t *Database) {
cfg, err := config.LoadDefaultConfig(context.TODO())
cfg.Region = "eu-central-1"
if err != nil {
fmt.Printf("Cannot log into DB: %s", err.Error())
t.Client = nil
return
}
client := dynamodb.NewFromConfig(cfg)
if client == nil {
fmt.Printf("Cannot log into DB: %s", err.Error())
t.Client = nil
return
}
t.Client = client
}
}

func WithTableName(n string) DatabaseOption {
return func(t *Database) {
t.TableName = n
Expand All @@ -73,26 +37,3 @@ func (t *Database) PutItem(item TableRecord, opts ...DatabaseOption) error {
}
return nil
}

func dynamoDBPutItem(client *dynamodb.Client, item TableRecord, table string) error {
_, err := client.DescribeTable(
context.TODO(), &dynamodb.DescribeTableInput{TableName: aws.String(table)})
if err != nil {
return fmt.Errorf("table error: %s", err.Error())
}

dbItem, err := attributevalue.MarshalMap(&item)
if err != nil {
panic(err)
}
fmt.Println("in ", dbItem["name"], dbItem["ca"])
fmt.Printf("table %s\n", table)
put_out, err := client.PutItem(context.TODO(), &dynamodb.PutItemInput{
TableName: aws.String(table), Item: dbItem,
})
fmt.Printf("out %v\n", put_out)
if err != nil {
log.Printf("Couldn't add item to table. Here's why: %v\n", err)
}
return nil
}
54 changes: 54 additions & 0 deletions src/cert-generator/pkg/aws/dynamoDb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package aws

import (
"context"
"fmt"
"log"

"github.com/aws/aws-sdk-go-v2/config"
"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/aws"
)

func WithDynamoDBLogin(region string) DatabaseOption {
return func(t *Database) {
cfg, err := config.LoadDefaultConfig(context.TODO())
cfg.Region = region
if err != nil {
fmt.Printf("Cannot log into DB: %s", err.Error())
t.Client = nil
return
}
client := dynamodb.NewFromConfig(cfg)
if client == nil {
fmt.Printf("Cannot log into DB: %s", err.Error())
t.Client = nil
return
}
t.Client = client
}
}

func dynamoDBPutItem(client *dynamodb.Client, item TableRecord, table string) error {
_, err := client.DescribeTable(
context.TODO(), &dynamodb.DescribeTableInput{TableName: aws.String(table)})
if err != nil {
return fmt.Errorf("table error: %s", err.Error())
}

if err != nil {
panic(err)
}
dbItem, err := attributevalue.MarshalMap(item)

if err != nil {
return fmt.Errorf("cannot marshal item into dynamoDBFormat")
}
_, err = client.PutItem(context.TODO(), &dynamodb.PutItemInput{
TableName: aws.String(table), Item: dbItem})
if err != nil {
log.Printf("Couldn't add item to table. Here's why: %v\n", err)
}
return nil
}
13 changes: 13 additions & 0 deletions src/cert-generator/pkg/aws/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package aws

type CertItem struct {
PrivateKey []byte `dynamodbav:"PrivateKey"`
Cert []byte `dynamodbav:"Cert"`
}

type TableRecord struct {
CaCert CertItem `dynamodbav:"Ca"`
CeCert CertItem `dynamodbav:"Ce"`
Name string `dynamodbav:"Name"`
CreationDate string `dynamodbav:"CreationDate"`
}
2 changes: 1 addition & 1 deletion terraform/dynamo_db.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resource "aws_dynamodb_table" "basic-dynamodb-table" {
resource "aws_dynamodb_table" "CertTable" {
name = "Certificates"
billing_mode = "PAY_PER_REQUEST"
hash_key = "Name"
Expand Down

0 comments on commit 607b607

Please sign in to comment.