| 1 | // Copyright 2023 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 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.25.7 `encoding/json`/" |
| 20 | "testingscip-go gomod github.com/golang/go/src go1.25.7 testing/" |
| 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/configscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/" |
| 24 | "github.com/sigstore/fulcio/pkg/identityscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity`/" |
| 25 | ) |
| 26 | |
| 27 | func TestIssuerscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/kubernetes`/TestIssuer().(t *testingscip-go gomod github.com/golang/go/src go1.25.7 testing/.Tscip-go gomod github.com/golang/go/src go1.25.7 testing/T#) { |
| 28 | ctx := contextscip-go gomod github.com/golang/go/src go1.25.7 context/.Backgroundscip-go gomod github.com/golang/go/src go1.25.7 context/Background().() |
| 29 | url := "test-issuer-url" |
| 30 | issuer := Issuerscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/kubernetes`/Issuer().(url) |
| 31 | |
| 32 | // test the Match function |
| 33 | t.Runscip-go gomod github.com/golang/go/src go1.25.7 testing/T#Run().("match", func(t *testingscip-go gomod github.com/golang/go/src go1.25.7 testing/.Tscip-go gomod github.com/golang/go/src go1.25.7 testing/T#) { |
| 34 | if matches := issuer.Matchscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity`/Issuer#Match.(ctx, url); !matches { |
| 35 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.7 testing/common#Fatal().("expected url to match but it doesn't") |
| 36 | } |
| 37 | if matches := issuer.Matchscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity`/Issuer#Match.(ctx, "some-other-url"); matches { |
| 38 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.7 testing/common#Fatal().("expected match to fail but it didn't") |
| 39 | } |
| 40 | }) |
| 41 | |
| 42 | t.Runscip-go gomod github.com/golang/go/src go1.25.7 testing/T#Run().("authenticate", func(t *testingscip-go gomod github.com/golang/go/src go1.25.7 testing/.Tscip-go gomod github.com/golang/go/src go1.25.7 testing/T#) { |
| 43 | 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#{ |
| 44 | Issuerscip-go gomod github.com/coreos/go-oidc/v3 v3.18.0 `github.com/coreos/go-oidc/v3/oidc`/IDToken#Issuer.: "https://iss.example.com", |
| 45 | Subjectscip-go gomod github.com/coreos/go-oidc/v3 v3.18.0 `github.com/coreos/go-oidc/v3/oidc`/IDToken#Subject.: "subject", |
| 46 | } |
| 47 | claims, err := jsonscip-go gomod github.com/golang/go/src go1.25.7 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.25.7 `encoding/json`/Marshal().(map[string]any{ |
| 48 | "aud": []string{"sigstore"}, |
| 49 | "iss": "https://iss.example.com", |
| 50 | "kubernetes.io": map[string]any{ |
| 51 | "namespace": "foo", |
| 52 | "pod": map[string]string{ |
| 53 | "name": "bar", |
| 54 | "uid": "2ff0bae1-6b8a-445b-ae03-1f8d2a08d031", |
| 55 | }, |
| 56 | "serviceaccount": map[string]string{ |
| 57 | "name": "baz", |
| 58 | "uid": "5cb6264f-e283-4365-9a1f-d5a15090527e", |
| 59 | }, |
| 60 | }, |
| 61 | "sub": "system:serviceaccount:foo:baz", |
| 62 | }) |
| 63 | if err != nil { |
| 64 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.7 testing/common#Fatal().(err) |
| 65 | } |
| 66 | withClaimsscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/kubernetes`/withClaims().(token, claims) |
| 67 | |
| 68 | identityscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity`/.Authorizescip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity`/Authorize. = func(_ 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, _ ...configscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/.InsecureOIDCConfigOptionscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/InsecureOIDCConfigOption#) (*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#, error) { |
| 69 | return token, nil |
| 70 | } |
| 71 | principal, err := issuer.Authenticatescip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity`/Issuer#Authenticate.(ctx, "token") |
| 72 | if err != nil { |
| 73 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.7 testing/common#Fatal().(err) |
| 74 | } |
| 75 | |
| 76 | if principal.Namescip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity`/Principal#Name.(ctx) != "subject" { |
| 77 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.7 testing/common#Fatalf().("got unexpected name %s", principal.Namescip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity`/Principal#Name.(ctx)) |
| 78 | } |
| 79 | }) |
| 80 | } |