|
| 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 gitlabcom |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "encoding/json" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/coreos/go-oidc/v3/oidc" |
| 23 | + "github.com/sigstore/fulcio/pkg/identity" |
| 24 | +) |
| 25 | + |
| 26 | +func TestIssuer(t *testing.T) { |
| 27 | + ctx := context.Background() |
| 28 | + url := "test-issuer-url" |
| 29 | + issuer := Issuer(url) |
| 30 | + |
| 31 | + // test the Match function |
| 32 | + t.Run("match", func(t *testing.T) { |
| 33 | + if matches := issuer.Match(ctx, url); !matches { |
| 34 | + t.Fatal("expected url to match but it doesn't") |
| 35 | + } |
| 36 | + if matches := issuer.Match(ctx, "some-other-url"); matches { |
| 37 | + t.Fatal("expected match to fail but it didn't") |
| 38 | + } |
| 39 | + }) |
| 40 | + |
| 41 | + t.Run("authenticate", func(t *testing.T) { |
| 42 | + token := &oidc.IDToken{ |
| 43 | + Issuer: "https://iss.example.com", |
| 44 | + Subject: "repo:sigstore/fulcio:ref:refs/heads/main", |
| 45 | + } |
| 46 | + claims, err := json.Marshal(map[string]interface{}{ |
| 47 | + "namespace_id": "1730270", |
| 48 | + "namespace_path": "cpanato", |
| 49 | + "project_id": "42831435", |
| 50 | + "project_path": "cpanato/testing-cosign", |
| 51 | + "user_id": "1430381", |
| 52 | + "user_login": "cpanato", |
| 53 | + "user_email": "[email protected]", |
| 54 | + "pipeline_id": "757451528", |
| 55 | + "pipeline_source": "push", |
| 56 | + "job_id": "3659681386", |
| 57 | + "sha": "714a629c0b401fdce83e847fc9589983fc6f46bc", |
| 58 | + "runner_id": 1, |
| 59 | + "runner_environment": "gitlab-hosted", |
| 60 | + "ref": "main", |
| 61 | + "ref_type": "branch", |
| 62 | + "ref_protected": "true", |
| 63 | + "jti": "914910cc-09f6-4217-8091-a1d3231a37db", |
| 64 | + "iss": "https://gitlab.com", |
| 65 | + "iat": 1674658264, |
| 66 | + "nbf": 1674658259, |
| 67 | + "exp": 1674661864, |
| 68 | + "sub": "project_path:cpanato/testing-cosign:ref_type:branch:ref:main", |
| 69 | + "aud": "sigstore", |
| 70 | + }) |
| 71 | + if err != nil { |
| 72 | + t.Fatal(err) |
| 73 | + } |
| 74 | + withClaims(token, claims) |
| 75 | + |
| 76 | + identity.Authorize = func(_ context.Context, _ string) (*oidc.IDToken, error) { |
| 77 | + return token, nil |
| 78 | + } |
| 79 | + principal, err := issuer.Authenticate(ctx, "token") |
| 80 | + if err != nil { |
| 81 | + t.Fatal(err) |
| 82 | + } |
| 83 | + |
| 84 | + if principal.Name(ctx) != "repo:sigstore/fulcio:ref:refs/heads/main" { |
| 85 | + t.Fatalf("got unexpected name %s", principal.Name(ctx)) |
| 86 | + } |
| 87 | + }) |
| 88 | +} |
0 commit comments