| 1 | package azuretoken |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "net/url" |
| 9 | "strings" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | type certRef struct { |
| 14 | KeyName, KeyVersion string |
| 15 | 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 certRef) KeyID() []byte { |
| 22 | return []byte(r.KeyName + "/" + r.KeyVersion) |
| 23 | } |
| 24 | |
| 25 | // decode a previous KeyID() call back into a key version |
| 26 | func refFromKeyID(keyID []byte) *certRef { |
| 27 | words := bytes.Split(keyID, []byte{'/'}) |
| 28 | if len(words) != 2 { |
| 29 | return nil |
| 30 | } |
| 31 | return &certRef{ |
| 32 | KeyName: string(words[0]), |
| 33 | KeyVersion: string(words[1]), |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // load the named certificate version and return the key it references |
| 38 | func (t *kvToken) loadCertificateVersion(ctx context.Context, baseURL, certName, certVersion string) (*certRef, error) { |
| 39 | cert, err := t.cli.GetCertificate(ctx, baseURL, certName, certVersion) |
| 40 | if err != nil { |
| 41 | return nil, err |
| 42 | } |
| 43 | if cert.Kid == nil { |
| 44 | return nil, errors.New("missing key ID in certificate") |
| 45 | } |
| 46 | keyWords, _, err := parseKeyURL(*cert.Kid) |
| 47 | if err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | if len(keyWords) != 4 || keyWords[1] != "keys" { |
| 51 | return nil, fmt.Errorf("unexpected format for key ID: %s", *cert.Kid) |
| 52 | } |
| 53 | var blob []byte |
| 54 | if cert.Cer != nil { |
| 55 | blob = *cert.Cer |
| 56 | } |
| 57 | return &certRef{ |
| 58 | KeyName: keyWords[2], |
| 59 | KeyVersion: keyWords[3], |
| 60 | 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 *kvToken) loadCertificateLatest(ctx context.Context, baseURL, certName string) (*certRef, error) { |
| 66 | // list all versions of the cert and pick the latest enabled one |
| 67 | certs, err := t.cli.GetCertificateVersions(ctx, baseURL, certName, nil) |
| 68 | if err != nil { |
| 69 | return nil, fmt.Errorf("listing cert versions: %w", err) |
| 70 | } |
| 71 | var best string |
| 72 | var bestNBF time.Time |
| 73 | for certs.NotDone() { |
| 74 | for _, cert := range certs.Values() { |
| 75 | if cert.Attributes == nil || cert.Attributes.Enabled == nil || !*cert.Attributes.Enabled { |
| 76 | continue |
| 77 | } |
| 78 | if cert.Attributes.NotBefore == nil || cert.ID == nil { |
| 79 | continue |
| 80 | } |
| 81 | nbf := time.Time(*cert.Attributes.NotBefore) |
| 82 | if nbf.After(bestNBF) { |
| 83 | best, bestNBF = *cert.ID, nbf |
| 84 | } |
| 85 | } |
| 86 | if err := certs.NextWithContext(ctx); err != nil { |
| 87 | return nil, fmt.Errorf("listing cert versions: %w", err) |
| 88 | } |
| 89 | } |
| 90 | // parse the cert ID and load it |
| 91 | if best == "" || bestNBF.IsZero() { |
| 92 | return nil, fmt.Errorf("cert %s has no enabled versions", certName) |
| 93 | } |
| 94 | words, _, err := parseKeyURL(best) |
| 95 | if err != nil { |
| 96 | return nil, fmt.Errorf("cert %s has invalid key ID: %w", certName, err) |
| 97 | } |
| 98 | if len(words) != 4 || words[1] != "certificates" { |
| 99 | return nil, fmt.Errorf("unexpected format for certificate ID: %s", best) |
| 100 | } |
| 101 | return t.loadCertificateVersion(ctx, baseURL, words[2], words[3]) |
| 102 | } |
| 103 | |
| 104 | var errKeyID = errors.New("id: expected URL of a certificate, certificate version, or key version") |
| 105 | |
| 106 | func parseKeyURL(keyURL string) (words []string, baseURL string, err error) { |
| 107 | if keyURL == "" { |
| 108 | return nil, "", errKeyID |
| 109 | } |
| 110 | // deconstruct URL to call GetKey so it can put it back together again |
| 111 | u, err := url.Parse(keyURL) |
| 112 | if err != nil { |
| 113 | return nil, "", fmt.Errorf("id: %w", err) |
| 114 | } else if u.Scheme == "" || u.Host == "" { |
| 115 | return nil, "", errKeyID |
| 116 | } |
| 117 | words = strings.Split(u.Path, "/") |
| 118 | u.Path = "" |
| 119 | baseURL = u.String() |
| 120 | return words, baseURL, nil |
| 121 | } |