Skip to content

Commit

Permalink
Add logger remove remained fmt.Printf
Browse files Browse the repository at this point in the history
  • Loading branch information
Fanni1993 committed Jun 17, 2021
1 parent 03b9c93 commit d0cb902
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
16 changes: 14 additions & 2 deletions venafi-snowflake-connector/lambda/request_cert/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/x509/pkix"
"encoding/json"
"fmt"
"os"
"strings"

"github.com/Venafi/vcert/v4"
Expand Down Expand Up @@ -32,6 +33,8 @@ type SnowFlakeType struct {

func RequestCert(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {

log.AddTarget(os.Stdout, log.LevelDebug)

var dataForRequestCert VenafiConnectorConfig
var snowflakeData SnowFlakeType
err := json.Unmarshal([]byte(request.Body), &snowflakeData)
Expand All @@ -43,12 +46,16 @@ func RequestCert(ctx context.Context, request events.APIGatewayProxyRequest) (ev
}, nil
}

// Parse parameters sent by Snowflake from Lambda Event
dataForRequestCert.TppURL = fmt.Sprintf("%v", snowflakeData.Data[0][1])
dataForRequestCert.DNSName = fmt.Sprintf("%v", snowflakeData.Data[0][2]) // TODO: UPN, DNS should allow multiple values
dataForRequestCert.Zone = fmt.Sprintf("%v", snowflakeData.Data[0][3])
dataForRequestCert.UPN = fmt.Sprintf("%v", snowflakeData.Data[0][4])
dataForRequestCert.CommonName = fmt.Sprintf("%v", snowflakeData.Data[0][5])

log.Infof("Finished parse parameters from event object")

// Get access token from S3. If access token is expired, generate a new one.
accessToken, err := utils.GetAccessToken(dataForRequestCert.TppURL)
if err != nil {
log.Errorf("Failed to get accesss token: %s", err)
Expand All @@ -58,14 +65,16 @@ func RequestCert(ctx context.Context, request events.APIGatewayProxyRequest) (ev
}, nil
}

log.Info("Got valid access token from S3")

config := &vcert.Config{
ConnectorType: endpoint.ConnectorTypeTPP,
BaseUrl: dataForRequestCert.TppURL,
Zone: dataForRequestCert.Zone,
Credentials: &endpoint.Authentication{
AccessToken: accessToken},
}

// Create a new Connector for Venafi API calls
c, err := vcert.NewClient(config)
if err != nil {
log.Errorf("Failed to connect to endpoint: %s", err)
Expand All @@ -84,7 +93,6 @@ func RequestCert(ctx context.Context, request events.APIGatewayProxyRequest) (ev
UPNs: []string{dataForRequestCert.UPN},
DNSNames: []string{dataForRequestCert.DNSName},
}

err = c.GenerateRequest(nil, enrollReq)
if err != nil {
log.Errorf("Failed to generate request: %v ", err)
Expand All @@ -94,6 +102,8 @@ func RequestCert(ctx context.Context, request events.APIGatewayProxyRequest) (ev
}, nil
}

log.Info("Generate request was successful")
// Request a new certificate using Venafi API
requestID, err := c.RequestCertificate(enrollReq)
if err != nil {
log.Errorf("Failed to request certificate:: %v ", err)
Expand All @@ -103,7 +113,9 @@ func RequestCert(ctx context.Context, request events.APIGatewayProxyRequest) (ev
}, nil
}
log.Infof("Certificate request was successful. RequestID is: %s", requestID)

escaped_requestID := strings.Replace(fmt.Sprintf("%v", requestID), "\\", "\\\\", -1)
// Transform data to a form which is readable by Snowflake
return events.APIGatewayProxyResponse{ // Success HTTP response
Body: fmt.Sprintf("{'data': [[0, '%v']]}", escaped_requestID),
StatusCode: 200,
Expand Down
6 changes: 3 additions & 3 deletions venafi-snowflake-connector/lambda/utils/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func GetNewAccessToken(single_credential_for_tpp map[string]string) *map[string]

new_creds, err := c.RefreshAccessToken(&auth)
if err != nil {
fmt.Printf("err: %v", err.Error())
log.Errorf("err: %v", err.Error())
return nil
}
single_credential_for_tpp["access_token"] = new_creds.Access_token
Expand Down Expand Up @@ -138,9 +138,9 @@ func getCredentials(filename, bucket, zone string) ([]byte, error) {
Key: aws.String(filename),
})
if err != nil {
fmt.Printf("Failed to get credentials: %v", err)
log.Errorf("Failed to get credentials: %v", err)
return []byte{}, fmt.Errorf("failed to get credentials, %v", err)
}
fmt.Printf("file downloaded, %d bytes\n", n)
log.Debugf("file downloaded, %d bytes\n", n)
return buff.Bytes(), nil
}

0 comments on commit d0cb902

Please sign in to comment.