| 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 spiffescip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/spiffe`/ |
| 16 | |
| 17 | import ( |
| 18 | "contextscip-go gomod github.com/golang/go/src go1.25.7 context/" |
| 19 | "testingscip-go gomod github.com/golang/go/src go1.25.7 testing/" |
| 20 | |
| 21 | "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`/" |
| 22 | "github.com/sigstore/fulcio/pkg/configscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/" |
| 23 | "github.com/sigstore/fulcio/pkg/identityscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity`/" |
| 24 | ) |
| 25 | |
| 26 | func TestIssuerscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/spiffe`/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#) { |
| 27 | 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().() |
| 28 | url := "test-issuer-url" |
| 29 | issuer := Issuerscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity/spiffe`/Issuer().(url) |
| 30 | |
| 31 | // test the Match function |
| 32 | 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#) { |
| 33 | if matches := issuer.Matchscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity`/Issuer#Match.(ctx, url); !matches { |
| 34 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.7 testing/common#Fatal().("expected url to match but it doesn't") |
| 35 | } |
| 36 | if matches := issuer.Matchscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity`/Issuer#Match.(ctx, "some-other-url"); matches { |
| 37 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.7 testing/common#Fatal().("expected match to fail but it didn't") |
| 38 | } |
| 39 | }) |
| 40 | |
| 41 | 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#) { |
| 42 | 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#{ |
| 43 | Issuerscip-go gomod github.com/coreos/go-oidc/v3 v3.18.0 `github.com/coreos/go-oidc/v3/oidc`/IDToken#Issuer.: "https://issuer.example.com", |
| 44 | Subjectscip-go gomod github.com/coreos/go-oidc/v3 v3.18.0 `github.com/coreos/go-oidc/v3/oidc`/IDToken#Subject.: "spiffe://example.com/foo/bar", |
| 45 | } |
| 46 | |
| 47 | cfg := &configscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/.FulcioConfigscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/FulcioConfig#{ |
| 48 | OIDCIssuersscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/FulcioConfig#OIDCIssuers.: map[string]configscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/.OIDCIssuerscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/OIDCIssuer#{ |
| 49 | "https://issuer.example.com": { |
| 50 | IssuerURLscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/OIDCIssuer#IssuerURL.: "https://issuer.example.com", |
| 51 | ClientIDscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/OIDCIssuer#ClientID.: "sigstore", |
| 52 | Typescip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/OIDCIssuer#Type.: "spiffe", |
| 53 | SPIFFETrustDomainscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/OIDCIssuer#SPIFFETrustDomain.: "example.com", |
| 54 | }, |
| 55 | }, |
| 56 | } |
| 57 | ctx := configscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/.Withscip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/config`/With().(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().(), cfg) |
| 58 | |
| 59 | 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) { |
| 60 | return token, nil |
| 61 | } |
| 62 | principal, err := issuer.Authenticatescip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity`/Issuer#Authenticate.(ctx, "token") |
| 63 | if err != nil { |
| 64 | t.Fatalscip-go gomod github.com/golang/go/src go1.25.7 testing/common#Fatal().(err) |
| 65 | } |
| 66 | |
| 67 | if principal.Namescip-go gomod github.com/sigstore/fulcio . `github.com/sigstore/fulcio/pkg/identity`/Principal#Name.(ctx) != "spiffe://example.com/foo/bar" { |
| 68 | 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)) |
| 69 | } |
| 70 | }) |
| 71 | } |