| 1 | // Copyright 2022 The Sigstore Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package kubernetesscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/kubernetes`/ |
| 16 | |
| 17 | import ( |
| 18 | "contextscip-go gomod github.com/golang/go/src go1.25.7 context/" |
| 19 | "crypto/x509scip-go gomod github.com/golang/go/src go1.25.7 `crypto/x509`/" |
| 20 | "net/urlscip-go gomod github.com/golang/go/src go1.25.7 `net/url`/" |
| 21 | |
| 22 | "github.com/coreos/go-oidc/v3/oidcscip-go gomod github.com/coreos/go-oidc/v3 v3.18.0 `github.com/coreos/go-oidc/v3/oidc`/" |
| 23 | "github.com/sigstore/fulcio/pkg/certificatescip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/certificate`/" |
| 24 | "github.com/sigstore/fulcio/pkg/identityscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity`/" |
| 25 | ) |
| 26 | |
| 27 | type principalscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/kubernetes`/principal# struct { |
| 28 | // Subject ('sub') from ID token |
| 29 | subjectscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/kubernetes`/principal#subject. string |
| 30 | |
| 31 | // Issuer ('iss') from ID token |
| 32 | issuerscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/kubernetes`/principal#issuer. string |
| 33 | |
| 34 | // URI to be set in certificate. URI is of the form |
| 35 | // https://kubernetes.io/namespaces/<namespace>/serviceaccounts/<serviceaccount>. |
| 36 | uriscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/kubernetes`/principal#uri. string |
| 37 | } |
| 38 | |
| 39 | func PrincipalFromIDTokenscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/kubernetes`/PrincipalFromIDToken().(_ contextscip-go gomod github.com/golang/go/src go1.25.7 context/.Contextscip-go gomod github.com/golang/go/src go1.25.7 context/Context#, token *oidcscip-go gomod github.com/coreos/go-oidc/v3 v3.18.0 `github.com/coreos/go-oidc/v3/oidc`/.IDTokenscip-go gomod github.com/coreos/go-oidc/v3 v3.18.0 `github.com/coreos/go-oidc/v3/oidc`/IDToken#) (identityscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity`/.Principalscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity`/Principal#, error) { |
| 40 | k8sURI, err := kubernetesTokenscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/kubernetes`/kubernetesToken().(token) |
| 41 | if err != nil { |
| 42 | return nil, err |
| 43 | } |
| 44 | return principalscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/kubernetes`/principal#{ |
| 45 | subjectscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/kubernetes`/principal#subject.: token.Subjectscip-go gomod github.com/coreos/go-oidc/v3 v3.18.0 `github.com/coreos/go-oidc/v3/oidc`/IDToken#Subject., |
| 46 | issuerscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/kubernetes`/principal#issuer.: token.Issuerscip-go gomod github.com/coreos/go-oidc/v3 v3.18.0 `github.com/coreos/go-oidc/v3/oidc`/IDToken#Issuer., |
| 47 | uriscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/kubernetes`/principal#uri.: k8sURI, |
| 48 | }, nil |
| 49 | } |
| 50 | |
| 51 | func (p principalscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/kubernetes`/principal#) Namescip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/kubernetes`/principal#Name().(contextscip-go gomod github.com/golang/go/src go1.25.7 context/.Contextscip-go gomod github.com/golang/go/src go1.25.7 context/Context#) string { |
| 52 | return p.subjectscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/kubernetes`/principal#subject. |
| 53 | } |
| 54 | |
| 55 | func (p principalscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/kubernetes`/principal#) Embedscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/kubernetes`/principal#Embed().(_ contextscip-go gomod github.com/golang/go/src go1.25.7 context/.Contextscip-go gomod github.com/golang/go/src go1.25.7 context/Context#, cert *x509scip-go gomod github.com/golang/go/src go1.25.7 `crypto/x509`/.Certificatescip-go gomod github.com/golang/go/src go1.25.7 `crypto/x509`/Certificate#) error { |
| 56 | parsed, err := urlscip-go gomod github.com/golang/go/src go1.25.7 `net/url`/.Parsescip-go gomod github.com/golang/go/src go1.25.7 `net/url`/Parse().(p.uriscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/kubernetes`/principal#uri.) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | cert.URIsscip-go gomod github.com/golang/go/src go1.25.7 `crypto/x509`/Certificate#URIs. = []*urlscip-go gomod github.com/golang/go/src go1.25.7 `net/url`/.URLscip-go gomod github.com/golang/go/src go1.25.7 `net/url`/URL#{parsed} |
| 61 | |
| 62 | cert.ExtraExtensionsscip-go gomod github.com/golang/go/src go1.25.7 `crypto/x509`/Certificate#ExtraExtensions., err = certificatescip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/certificate`/.Extensionsscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/certificate`/Extensions#{ |
| 63 | Issuerscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/certificate`/Extensions#Issuer.: p.issuerscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/kubernetes`/principal#issuer., |
| 64 | Subjectscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/certificate`/Extensions#Subject.: p.subjectscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/kubernetes`/principal#subject., |
| 65 | }.Renderscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/certificate`/Extensions#Render().() |
| 66 | if err != nil { |
| 67 | return err |
| 68 | } |
| 69 | |
| 70 | return nil |
| 71 | } |
| 72 | |
| 73 | func kubernetesTokenscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/kubernetes`/kubernetesToken().(token *oidcscip-go gomod github.com/coreos/go-oidc/v3 v3.18.0 `github.com/coreos/go-oidc/v3/oidc`/.IDTokenscip-go gomod github.com/coreos/go-oidc/v3 v3.18.0 `github.com/coreos/go-oidc/v3/oidc`/IDToken#) (string, error) { |
| 74 | // Extract custom claims |
| 75 | var claims struct { |
| 76 | // "kubernetes.io": { |
| 77 | // "namespace": "default", |
| 78 | // "pod": { |
| 79 | // "name": "oidc-test", |
| 80 | // "uid": "49ad3572-b3dd-43a6-8d77-5858d3660275" |
| 81 | // }, |
| 82 | // "serviceaccount": { |
| 83 | // "name": "default", |
| 84 | // "uid": "f5720c1d-e152-4356-a897-11b07aff165d" |
| 85 | // } |
| 86 | // } |
| 87 | Kubernetes struct { |
| 88 | Namespace string `json:"namespace"` |
| 89 | Pod struct { |
| 90 | Name string `json:"name"` |
| 91 | UID string `json:"uid"` |
| 92 | } `json:"pod"` |
| 93 | ServiceAccount struct { |
| 94 | Name string `json:"name"` |
| 95 | UID string `json:"uid"` |
| 96 | } `json:"serviceaccount"` |
| 97 | } `json:"kubernetes.io"` |
| 98 | } |
| 99 | if err := token.Claimsscip-go gomod github.com/coreos/go-oidc/v3 v3.18.0 `github.com/coreos/go-oidc/v3/oidc`/IDToken#Claims().(&claims); err != nil { |
| 100 | return "", err |
| 101 | } |
| 102 | |
| 103 | // We use this in URIs, so it has to be a URI. |
| 104 | return "https://kubernetes.io/namespaces/" + claims.Kubernetes.Namespace + "/serviceaccounts/" + claims.Kubernetes.ServiceAccount.Name, nil |
| 105 | } |