| 1 | package azuretokenscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/ |
| 2 | |
| 3 | import ( |
| 4 | "bytesscip-go gomod github.com/golang/go/src go1.18 bytes/" |
| 5 | "contextscip-go gomod github.com/golang/go/src go1.18 context/" |
| 6 | "errorsscip-go gomod github.com/golang/go/src go1.18 errors/" |
| 7 | "fmtscip-go gomod github.com/golang/go/src go1.18 fmt/" |
| 8 | "net/urlscip-go gomod github.com/golang/go/src go1.18 `net/url`/" |
| 9 | "stringsscip-go gomod github.com/golang/go/src go1.18 strings/" |
| 10 | "timescip-go gomod github.com/golang/go/src go1.18 time/" |
| 11 | ) |
| 12 | |
| 13 | type certRefscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/certRef# struct { |
| 14 | KeyNamescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/certRef#KeyName., KeyVersionscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/certRef#KeyVersion. string |
| 15 | CertBlobscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/certRef#CertBlob. []byte |
| 16 | } |
| 17 | |
| 18 | // encode the exact key version into our ID field so that when we're being used |
| 19 | // via a worker token, callers get a consistent signing key from GetKey() to |
| 20 | // Sign() |
| 21 | func (r certRefscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/certRef#) KeyIDscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/certRef#KeyID().() []byte { |
| 22 | return []byte(r.KeyNamescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/certRef#KeyName. + "/" + r.KeyVersionscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/certRef#KeyVersion.) |
| 23 | } |
| 24 | |
| 25 | // decode a previous KeyID() call back into a key version |
| 26 | func refFromKeyIDscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/refFromKeyID().(keyID []byte) *certRefscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/certRef# { |
| 27 | words := bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.Splitscip-go gomod github.com/golang/go/src go1.18 bytes/Split().(keyID, []byte{'/'}) |
| 28 | if len(words) != 2 { |
| 29 | return nil |
| 30 | } |
| 31 | return &certRefscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/certRef#{ |
| 32 | KeyNamescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/certRef#KeyName.: string(words[0]), |
| 33 | KeyVersionscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/certRef#KeyVersion.: string(words[1]), |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // load the named certificate version and return the key it references |
| 38 | func (t *kvTokenscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/kvToken#) loadCertificateVersionscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/kvToken#loadCertificateVersion().(ctx contextscip-go gomod github.com/golang/go/src go1.18 context/.Contextscip-go gomod github.com/golang/go/src go1.18 context/Context#, baseURL, certName, certVersion string) (*certRefscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/certRef#, error) { |
| 39 | cert, err := t.cliscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/kvToken#cli..GetCertificatescip-go gomod github.com/Azure/azure-sdk-for-go v68.0.0 `github.com/Azure/azure-sdk-for-go/services/keyvault/2016-10-01/keyvault`/BaseClient#GetCertificate().(ctx, baseURL, certName, certVersion) |
| 40 | if err != nil { |
| 41 | return nil, err |
| 42 | } |
| 43 | if cert.Kidscip-go gomod github.com/Azure/azure-sdk-for-go v68.0.0 `github.com/Azure/azure-sdk-for-go/services/keyvault/2016-10-01/keyvault`/CertificateBundle#Kid. == nil { |
| 44 | return nil, errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Newscip-go gomod github.com/golang/go/src go1.18 errors/New().("missing key ID in certificate") |
| 45 | } |
| 46 | keyWords, _, err := parseKeyURLscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/parseKeyURL().(*cert.Kidscip-go gomod github.com/Azure/azure-sdk-for-go v68.0.0 `github.com/Azure/azure-sdk-for-go/services/keyvault/2016-10-01/keyvault`/CertificateBundle#Kid.) |
| 47 | if err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | if len(keyWords) != 4 || keyWords[1] != "keys" { |
| 51 | return nil, fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("unexpected format for key ID: %s", *cert.Kidscip-go gomod github.com/Azure/azure-sdk-for-go v68.0.0 `github.com/Azure/azure-sdk-for-go/services/keyvault/2016-10-01/keyvault`/CertificateBundle#Kid.) |
| 52 | } |
| 53 | var blob []byte |
| 54 | if cert.Cerscip-go gomod github.com/Azure/azure-sdk-for-go v68.0.0 `github.com/Azure/azure-sdk-for-go/services/keyvault/2016-10-01/keyvault`/CertificateBundle#Cer. != nil { |
| 55 | blob = *cert.Cerscip-go gomod github.com/Azure/azure-sdk-for-go v68.0.0 `github.com/Azure/azure-sdk-for-go/services/keyvault/2016-10-01/keyvault`/CertificateBundle#Cer. |
| 56 | } |
| 57 | return &certRefscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/certRef#{ |
| 58 | KeyNamescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/certRef#KeyName.: keyWords[2], |
| 59 | KeyVersionscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/certRef#KeyVersion.: keyWords[3], |
| 60 | CertBlobscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/certRef#CertBlob.: blob, |
| 61 | }, nil |
| 62 | } |
| 63 | |
| 64 | // load the latest enabled version of the named certificate and return the key it references |
| 65 | func (t *kvTokenscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/kvToken#) loadCertificateLatestscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/kvToken#loadCertificateLatest().(ctx contextscip-go gomod github.com/golang/go/src go1.18 context/.Contextscip-go gomod github.com/golang/go/src go1.18 context/Context#, baseURL, certName string) (*certRefscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/certRef#, error) { |
| 66 | // list all versions of the cert and pick the latest enabled one |
| 67 | certs, err := t.cliscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/kvToken#cli..GetCertificateVersionsscip-go gomod github.com/Azure/azure-sdk-for-go v68.0.0 `github.com/Azure/azure-sdk-for-go/services/keyvault/2016-10-01/keyvault`/BaseClient#GetCertificateVersions().(ctx, baseURL, certName, nil) |
| 68 | if err != nil { |
| 69 | return nil, fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("listing cert versions: %w", err) |
| 70 | } |
| 71 | var best string |
| 72 | var bestNBF timescip-go gomod github.com/golang/go/src go1.18 time/.Timescip-go gomod github.com/golang/go/src go1.18 time/Time# |
| 73 | for certs.NotDonescip-go gomod github.com/Azure/azure-sdk-for-go v68.0.0 `github.com/Azure/azure-sdk-for-go/services/keyvault/2016-10-01/keyvault`/CertificateListResultPage#NotDone().() { |
| 74 | for _, cert := range certs.Valuesscip-go gomod github.com/Azure/azure-sdk-for-go v68.0.0 `github.com/Azure/azure-sdk-for-go/services/keyvault/2016-10-01/keyvault`/CertificateListResultPage#Values().() { |
| 75 | if cert.Attributesscip-go gomod github.com/Azure/azure-sdk-for-go v68.0.0 `github.com/Azure/azure-sdk-for-go/services/keyvault/2016-10-01/keyvault`/CertificateItem#Attributes. == nil || cert.Attributesscip-go gomod github.com/Azure/azure-sdk-for-go v68.0.0 `github.com/Azure/azure-sdk-for-go/services/keyvault/2016-10-01/keyvault`/CertificateItem#Attributes..Enabledscip-go gomod github.com/Azure/azure-sdk-for-go v68.0.0 `github.com/Azure/azure-sdk-for-go/services/keyvault/2016-10-01/keyvault`/CertificateAttributes#Enabled. == nil || !*cert.Attributesscip-go gomod github.com/Azure/azure-sdk-for-go v68.0.0 `github.com/Azure/azure-sdk-for-go/services/keyvault/2016-10-01/keyvault`/CertificateItem#Attributes..Enabledscip-go gomod github.com/Azure/azure-sdk-for-go v68.0.0 `github.com/Azure/azure-sdk-for-go/services/keyvault/2016-10-01/keyvault`/CertificateAttributes#Enabled. { |
| 76 | continue |
| 77 | } |
| 78 | if cert.Attributesscip-go gomod github.com/Azure/azure-sdk-for-go v68.0.0 `github.com/Azure/azure-sdk-for-go/services/keyvault/2016-10-01/keyvault`/CertificateItem#Attributes..NotBeforescip-go gomod github.com/Azure/azure-sdk-for-go v68.0.0 `github.com/Azure/azure-sdk-for-go/services/keyvault/2016-10-01/keyvault`/CertificateAttributes#NotBefore. == nil || cert.IDscip-go gomod github.com/Azure/azure-sdk-for-go v68.0.0 `github.com/Azure/azure-sdk-for-go/services/keyvault/2016-10-01/keyvault`/CertificateItem#ID. == nil { |
| 79 | continue |
| 80 | } |
| 81 | nbf := timescip-go gomod github.com/golang/go/src go1.18 time/.Timescip-go gomod github.com/golang/go/src go1.18 time/Time#(*cert.Attributesscip-go gomod github.com/Azure/azure-sdk-for-go v68.0.0 `github.com/Azure/azure-sdk-for-go/services/keyvault/2016-10-01/keyvault`/CertificateItem#Attributes..NotBeforescip-go gomod github.com/Azure/azure-sdk-for-go v68.0.0 `github.com/Azure/azure-sdk-for-go/services/keyvault/2016-10-01/keyvault`/CertificateAttributes#NotBefore.) |
| 82 | if nbf.Afterscip-go gomod github.com/golang/go/src go1.18 time/Time#After().(bestNBF) { |
| 83 | best, bestNBF = *cert.IDscip-go gomod github.com/Azure/azure-sdk-for-go v68.0.0 `github.com/Azure/azure-sdk-for-go/services/keyvault/2016-10-01/keyvault`/CertificateItem#ID., nbf |
| 84 | } |
| 85 | } |
| 86 | if err := certs.NextWithContextscip-go gomod github.com/Azure/azure-sdk-for-go v68.0.0 `github.com/Azure/azure-sdk-for-go/services/keyvault/2016-10-01/keyvault`/CertificateListResultPage#NextWithContext().(ctx); err != nil { |
| 87 | return nil, fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("listing cert versions: %w", err) |
| 88 | } |
| 89 | } |
| 90 | // parse the cert ID and load it |
| 91 | if best == "" || bestNBF.IsZeroscip-go gomod github.com/golang/go/src go1.18 time/Time#IsZero().() { |
| 92 | return nil, fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("cert %s has no enabled versions", certName) |
| 93 | } |
| 94 | words, _, err := parseKeyURLscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/parseKeyURL().(best) |
| 95 | if err != nil { |
| 96 | return nil, fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("cert %s has invalid key ID: %w", certName, err) |
| 97 | } |
| 98 | if len(words) != 4 || words[1] != "certificates" { |
| 99 | return nil, fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("unexpected format for certificate ID: %s", best) |
| 100 | } |
| 101 | return t.loadCertificateVersionscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/kvToken#loadCertificateVersion().(ctx, baseURL, words[2], words[3]) |
| 102 | } |
| 103 | |
| 104 | var errKeyIDscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/errKeyID. = errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Newscip-go gomod github.com/golang/go/src go1.18 errors/New().("id: expected URL of a certificate, certificate version, or key version") |
| 105 | |
| 106 | func parseKeyURLscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/parseKeyURL().(keyURL string) (words []string, baseURL string, err error) { |
| 107 | if keyURL == "" { |
| 108 | return nil, "", errKeyIDscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/errKeyID. |
| 109 | } |
| 110 | // deconstruct URL to call GetKey so it can put it back together again |
| 111 | u, err := urlscip-go gomod github.com/golang/go/src go1.18 `net/url`/.Parsescip-go gomod github.com/golang/go/src go1.18 `net/url`/Parse().(keyURL) |
| 112 | if err != nil { |
| 113 | return nil, "", fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("id: %w", err) |
| 114 | } else if u.Schemescip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#Scheme. == "" || u.Hostscip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#Host. == "" { |
| 115 | return nil, "", errKeyIDscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/token/azuretoken`/errKeyID. |
| 116 | } |
| 117 | words = stringsscip-go gomod github.com/golang/go/src go1.18 strings/.Splitscip-go gomod github.com/golang/go/src go1.18 strings/Split().(u.Pathscip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#Path., "/") |
| 118 | u.Pathscip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#Path. = "" |
| 119 | baseURL = u.Stringscip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#String().() |
| 120 | return words, baseURL, nil |
| 121 | } |