Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev/robin/9209 azkeys and cose to log chatty #53

Merged
merged 4 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions azkeys/coseSigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"time"

"github.com/Azure/azure-sdk-for-go/services/keyvault/2016-10-01/keyvault"
"github.com/datatrails/go-datatrails-common/logger"
"github.com/veraison/go-cose"
)

Expand Down Expand Up @@ -62,7 +61,6 @@ func base64BEtoBigInt(in string) (*big.Int, error) {

// PublicKey gets the latest key's public key
func (kv *CoseSignerKeyVault) PublicKey() (*ecdsa.PublicKey, error) {
logger.Sugar.Infof("PublicKey: %s %s", kv.url, kv.keyName)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

Expand Down Expand Up @@ -97,7 +95,6 @@ func (kv *CoseSignerKeyVault) PublicKey() (*ecdsa.PublicKey, error) {
// Sign signs a given content
func (kv *CoseSignerKeyVault) Sign(rand io.Reader, content []byte) ([]byte, error) {

logger.Sugar.Infof("Sign: %s %s", kv.url, kv.keyName)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

Expand Down
35 changes: 0 additions & 35 deletions azkeys/keyvault.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ func (kv *KeyVault) GetKeyByKID(
ctx context.Context, keyID string,
) (keyvault.KeyBundle, error) {

log := logger.Sugar.FromContext(ctx)
defer log.Close()

log.Infof("GetKeyByKID: %s %s", kv.url, keyID)

kvClient, err := NewKvClient(kv.Authorizer)
if err != nil {
return keyvault.KeyBundle{}, err
Expand All @@ -67,11 +62,6 @@ func (kv *KeyVault) GetLatestKey(
ctx context.Context, keyName string,
) (keyvault.KeyBundle, error) {

log := logger.Sugar.FromContext(ctx)
defer log.Close()

log.Infof("GetLatestKey: %s %s", kv.url, keyName)

kvClient, err := NewKvClient(kv.Authorizer)
if err != nil {
return keyvault.KeyBundle{}, err
Expand All @@ -95,8 +85,6 @@ func (kv *KeyVault) GetKeyVersionsKeys(
log := logger.Sugar.FromContext(ctx)
defer log.Close()

log.Infof("GetKeyVersions: %s %s", kv.url, keyID)

kvClient, err := NewKvClient(kv.Authorizer)
if err != nil {
return []keyvault.KeyBundle{}, err
Expand Down Expand Up @@ -140,11 +128,6 @@ func (kv *KeyVault) GetKeyVersionsKeys(
// getKeysFromVersions gets the keys from the given key versions
func (kv *KeyVault) getKeysFromVersions(ctx context.Context, keyVersions []keyvault.KeyItem) ([]keyvault.KeyBundle, error) {

log := logger.Sugar.FromContext(ctx)
defer log.Close()

log.Infof("getKeysFromVersions")

keys := []keyvault.KeyBundle{}

for _, keyVersionValue := range keyVersions {
Expand Down Expand Up @@ -194,20 +177,13 @@ func (kv *KeyVault) Sign(
algorithm keyvault.JSONWebKeySignatureAlgorithm,
) ([]byte, error) {

log := logger.Sugar.FromContext(ctx)
defer log.Close()

log.Infof("Sign: %s %s", kv.url, keyID)

kvClient, err := NewKvClient(kv.Authorizer)
if err != nil {
return []byte{}, fmt.Errorf("failed to create keyvault client: %w", err)
}

payloadStr := base64.URLEncoding.EncodeToString(payload)

logger.Sugar.Infof("Payload Str: %v", payloadStr)

params := keyvault.KeySignParameters{
Algorithm: algorithm,
Value: &payloadStr,
Expand All @@ -220,7 +196,6 @@ func (kv *KeyVault) Sign(
return []byte{}, fmt.Errorf("failed to sign payload: %w", err)
}

logger.Sugar.Infof("SignatureB64: %v", *signatureb64.Result)
signature, err := base64.URLEncoding.DecodeString(*signatureb64.Result)

return signature, err
Expand Down Expand Up @@ -254,11 +229,6 @@ func (kv *KeyVault) HashAndSign(
algorithm keyvault.JSONWebKeySignatureAlgorithm,
) ([]byte, error) {

log := logger.Sugar.FromContext(ctx)
defer log.Close()

log.Infof("HashAndSign: %s %s", kv.url, keyID)

key, err := kv.GetKeyByKID(ctx, keyID)
if err != nil {
return nil, fmt.Errorf("failed to get key: %w", err)
Expand Down Expand Up @@ -287,11 +257,6 @@ func (kv *KeyVault) Verify(
algorithm keyvault.JSONWebKeySignatureAlgorithm,
) (bool, error) {

log := logger.Sugar.FromContext(ctx)
defer log.Close()

log.Infof("Verify: %s %s", kv.url, keyID)

kvClient, err := NewKvClient(kv.Authorizer)
if err != nil {
return false, fmt.Errorf("failed to create keyvault client: %w", err)
Expand Down
8 changes: 4 additions & 4 deletions azkeys/secretvault.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ func getWithDefault(key string) string {

func EnvironmentAuthorizer() (autorest.Authorizer, error) {

logger.Sugar.Infof("Using env authorizer for keyvault")
logger.Sugar.Infof("AZURE_TENANT_ID: %s", getWithDefault("AZURE_TENANT_ID"))
logger.Sugar.Infof("AZURE_CLIENT_ID: %s", getWithDefault("AZURE_CLIENT_ID"))
logger.Sugar.Debugf("Using env authorizer for keyvault")
logger.Sugar.Debugf("AZURE_TENANT_ID: %s", getWithDefault("AZURE_TENANT_ID"))
logger.Sugar.Debugf("AZURE_CLIENT_ID: %s", getWithDefault("AZURE_CLIENT_ID"))
// We do not use the env auhtorizer in production
logger.Sugar.Infof("AZURE_CLIENT_SECRET: %s", getWithDefault("AZURE_CLIENT_SECRET"))
logger.Sugar.Debugf("AZURE_CLIENT_SECRET: %s", getWithDefault("AZURE_CLIENT_SECRET"))

return auth.NewAuthorizerFromEnvironment()
}
Expand Down
9 changes: 6 additions & 3 deletions cose/cose.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,28 +196,30 @@ func (cs *CoseSign1Message) CWTClaimsFromProtectedHeader() (*CWTClaims, error) {
return nil, &ErrUnexpectedProtectedHeaderType{label: HeaderLabelCWTClaims, expectedType: "map[interface{}]interface{}", actualType: reflect.TypeOf(cwtClaimsMap).String()}
}

logger.Sugar.Infof("CWT Claims: %v", cwtClaimsMap)

issuer, ok := cwtClaimsMap[int64(cwt.KeyIss)]
if !ok {
logger.Sugar.Infof("CWT Claims: %v", cwtClaimsMap)
logger.Sugar.Infof("CWTClaimsFromProtectedHeader: failed to get issuer from cwt claims: %v", err)
return nil, ErrCWTClaimsNoIssuer
}

issuerStr, ok := issuer.(string)
if !ok {
logger.Sugar.Infof("CWT Claims: %v", cwtClaimsMap)
logger.Sugar.Infof("CWTClaimsFromProtectedHeader: issuer is not string: %v", err)
return nil, ErrCWTClaimsIssuerNotString
}

subject, ok := cwtClaimsMap[int64(cwt.KeySub)]
if !ok {
logger.Sugar.Infof("CWT Claims: %v", cwtClaimsMap)
logger.Sugar.Infof("CWTClaimsFromProtectedHeader: failed to get subject from cwt claims: %v", err)
return nil, ErrCWTClaimsNoSubject
}

subjectStr, ok := subject.(string)
if !ok {
logger.Sugar.Infof("CWT Claims: %v", cwtClaimsMap)
logger.Sugar.Infof("CWTClaimsFromProtectedHeader: subject is not string: %v", err)
return nil, ErrCWTClaimsSubjectNotString
}
Expand Down Expand Up @@ -296,17 +298,18 @@ func (cs *CoseSign1Message) VerifyWithProvider(
if err != nil {
return err
}
logger.Sugar.Infof("verify: publicKey: %v, algorithm: %v", publicKey, algorithm)

verifier, err := cose.NewVerifier(algorithm, publicKey)
if err != nil {
logger.Sugar.Infof("verify: publicKey: %v, algorithm: %v", publicKey, algorithm)
logger.Sugar.Infof("verify: failed to make verifier from public key: %v", err)
return err
}

// verify the message
err = cs.Verify(external, verifier)
if err != nil {
logger.Sugar.Infof("verify: publicKey: %v, algorithm: %v", publicKey, algorithm)
logger.Sugar.Infof("verify: failed to verify message: %v", err)
return err
}
Expand Down
2 changes: 0 additions & 2 deletions cose/ec_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ func NewECCoseKey(coseKey map[int64]interface{}) (*ECCoseKey, error) {
// ECCoseKey
func (ecck *ECCoseKey) PublicKey() (crypto.PublicKey, error) {

logger.Sugar.Info("PublicKey: %v", ecck)

publicKey := ecdsa.PublicKey{}

// first find the curve
Expand Down
Loading