|
| 1 | +// |
| 2 | +// Copyright 2026 The Chainloop Authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package service |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "crypto/ecdsa" |
| 21 | + "crypto/elliptic" |
| 22 | + "crypto/rand" |
| 23 | + "crypto/x509" |
| 24 | + "encoding/pem" |
| 25 | + "os" |
| 26 | + "path/filepath" |
| 27 | + "testing" |
| 28 | + |
| 29 | + pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1" |
| 30 | + conf "github.com/chainloop-dev/chainloop/app/controlplane/internal/conf/controlplane/config/v1" |
| 31 | + "github.com/chainloop-dev/chainloop/app/controlplane/internal/usercontext" |
| 32 | + "github.com/chainloop-dev/chainloop/app/controlplane/pkg/authz" |
| 33 | + "github.com/chainloop-dev/chainloop/app/controlplane/pkg/biz" |
| 34 | + "github.com/chainloop-dev/chainloop/app/controlplane/pkg/biz/testhelpers" |
| 35 | + "github.com/chainloop-dev/chainloop/app/controlplane/pkg/usercontext/entities" |
| 36 | + "github.com/chainloop-dev/chainloop/pkg/blobmanager/oci" |
| 37 | + creds "github.com/chainloop-dev/chainloop/pkg/credentials/mocks" |
| 38 | + kerrors "github.com/go-kratos/kratos/v2/errors" |
| 39 | + "github.com/stretchr/testify/mock" |
| 40 | + "github.com/stretchr/testify/suite" |
| 41 | +) |
| 42 | + |
| 43 | +const ( |
| 44 | + digestProjectA = "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 45 | + digestProjectB = "sha256:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" |
| 46 | + digestOrgWide = "sha256:cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc" |
| 47 | +) |
| 48 | + |
| 49 | +// Regression tests for PFM-6716: a project-scoped API token must not be able to resolve a CAS |
| 50 | +// mapping outside its project through any of the two download endpoints, GetDownloadURL and |
| 51 | +// CASCredentialsService.Get, which share the same mapping lookup. |
| 52 | +type casDownloadRBACIntegrationSuite struct { |
| 53 | + testhelpers.UseCasesEachTestSuite |
| 54 | + org *biz.Organization |
| 55 | + projectA, projectB *biz.Project |
| 56 | + backend *biz.CASBackend |
| 57 | + projectToken *biz.APIToken |
| 58 | + orgToken *biz.APIToken |
| 59 | + redirectSvc *CASRedirectService |
| 60 | + credentialsSvc *CASCredentialsService |
| 61 | +} |
| 62 | + |
| 63 | +func (s *casDownloadRBACIntegrationSuite) SetupTest() { |
| 64 | + credsWriter := creds.NewReaderWriter(s.T()) |
| 65 | + credsWriter.On("SaveCredentials", mock.Anything, mock.Anything, mock.Anything).Return("stored-OCI-secret", nil) |
| 66 | + |
| 67 | + s.TestingUseCases = testhelpers.NewTestingUseCases(s.T(), testhelpers.WithCredsReaderWriter(credsWriter)) |
| 68 | + |
| 69 | + ctx := context.Background() |
| 70 | + var err error |
| 71 | + |
| 72 | + s.org, err = s.Organization.Create(ctx, "cas-download-rbac-org") |
| 73 | + s.Require().NoError(err) |
| 74 | + |
| 75 | + s.projectA, err = s.Project.Create(ctx, s.org.ID, "project-a") |
| 76 | + s.Require().NoError(err) |
| 77 | + s.projectB, err = s.Project.Create(ctx, s.org.ID, "project-b") |
| 78 | + s.Require().NoError(err) |
| 79 | + |
| 80 | + s.backend, err = s.CASBackend.Create(ctx, s.org.ID, "rbac-backend", "my-location", "backend for RBAC tests", oci.ProviderID, nil, true, false, nil) |
| 81 | + s.Require().NoError(err) |
| 82 | + |
| 83 | + // A token confined to project A and an org-wide token, both carrying the default |
| 84 | + // artifact-download policy. |
| 85 | + s.projectToken, err = s.APIToken.Create(ctx, "token-project-a", nil, nil, &s.org.ID, biz.APITokenWithProject(s.projectA)) |
| 86 | + s.Require().NoError(err) |
| 87 | + s.orgToken, err = s.APIToken.Create(ctx, "token-org", nil, nil, &s.org.ID) |
| 88 | + s.Require().NoError(err) |
| 89 | + |
| 90 | + // Artifacts scoped to each project plus one with no scope at all, reachable with |
| 91 | + // organization-wide access only. |
| 92 | + for digest, project := range map[string]*biz.Project{ |
| 93 | + digestProjectA: s.projectA, |
| 94 | + digestProjectB: s.projectB, |
| 95 | + } { |
| 96 | + _, err = s.CASMapping.Create(ctx, digest, s.backend.ID.String(), &biz.CASMappingCreateOpts{ProjectID: &project.ID}) |
| 97 | + s.Require().NoError(err) |
| 98 | + } |
| 99 | + |
| 100 | + _, err = s.CASMapping.Create(ctx, digestOrgWide, s.backend.ID.String(), nil) |
| 101 | + s.Require().NoError(err) |
| 102 | + |
| 103 | + // Services wired as in production, with a throwaway EC key for the CAS JWT builder. |
| 104 | + casUC, err := biz.NewCASCredentialsUseCase(&conf.Auth{CasRobotAccountPrivateKeyPath: s.writeECPrivateKey()}) |
| 105 | + s.Require().NoError(err) |
| 106 | + |
| 107 | + authzUC := biz.NewAuthzUseCase(&biz.AuthzUseCaseConfig{ |
| 108 | + CasbinEnforcer: s.Enforcer, |
| 109 | + APITokenRepo: s.Repos.APITokenRepo, |
| 110 | + Logger: s.L, |
| 111 | + }) |
| 112 | + |
| 113 | + s.redirectSvc, err = NewCASRedirectService(s.CASMapping, casUC, &conf.Bootstrap_CASServer{DownloadUrl: "https://cas.example.test/download"}) |
| 114 | + s.Require().NoError(err) |
| 115 | + |
| 116 | + s.credentialsSvc = NewCASCredentialsService(casUC, s.CASMapping, s.CASBackend, authzUC) |
| 117 | +} |
| 118 | + |
| 119 | +func (s *casDownloadRBACIntegrationSuite) TestGetDownloadURL() { |
| 120 | + s.Run("project-scoped token cannot download another project's artifact", func() { |
| 121 | + _, err := s.redirectSvc.GetDownloadURL(s.ctxForToken(s.projectToken), &pb.GetDownloadURLRequest{Digest: digestProjectB}) |
| 122 | + s.Require().Error(err) |
| 123 | + s.True(kerrors.IsNotFound(err), "expected not found, got %v", err) |
| 124 | + }) |
| 125 | + |
| 126 | + s.Run("project-scoped token cannot download an org-wide artifact", func() { |
| 127 | + _, err := s.redirectSvc.GetDownloadURL(s.ctxForToken(s.projectToken), &pb.GetDownloadURLRequest{Digest: digestOrgWide}) |
| 128 | + s.Require().Error(err) |
| 129 | + s.True(kerrors.IsNotFound(err), "expected not found, got %v", err) |
| 130 | + }) |
| 131 | + |
| 132 | + s.Run("project-scoped token can download its own project's artifact", func() { |
| 133 | + resp, err := s.redirectSvc.GetDownloadURL(s.ctxForToken(s.projectToken), &pb.GetDownloadURLRequest{Digest: digestProjectA}) |
| 134 | + s.Require().NoError(err) |
| 135 | + s.Contains(resp.GetResult().GetUrl(), digestProjectA) |
| 136 | + }) |
| 137 | + |
| 138 | + s.Run("org-scoped token still reaches the whole organization", func() { |
| 139 | + for _, digest := range []string{digestProjectA, digestProjectB, digestOrgWide} { |
| 140 | + resp, err := s.redirectSvc.GetDownloadURL(s.ctxForToken(s.orgToken), &pb.GetDownloadURLRequest{Digest: digest}) |
| 141 | + s.Require().NoError(err) |
| 142 | + s.Contains(resp.GetResult().GetUrl(), digest) |
| 143 | + } |
| 144 | + }) |
| 145 | +} |
| 146 | + |
| 147 | +func (s *casDownloadRBACIntegrationSuite) TestCASCredentialsGet() { |
| 148 | + s.Run("project-scoped token cannot get downloader credentials for another project's artifact", func() { |
| 149 | + _, err := s.credentialsSvc.Get(s.ctxForToken(s.projectToken), &pb.CASCredentialsServiceGetRequest{ |
| 150 | + Digest: digestProjectB, |
| 151 | + Role: pb.CASCredentialsServiceGetRequest_ROLE_DOWNLOADER, |
| 152 | + }) |
| 153 | + s.Require().Error(err) |
| 154 | + s.True(kerrors.IsForbidden(err), "expected forbidden, got %v", err) |
| 155 | + }) |
| 156 | + |
| 157 | + s.Run("project-scoped token cannot get downloader credentials for an org-wide artifact", func() { |
| 158 | + _, err := s.credentialsSvc.Get(s.ctxForToken(s.projectToken), &pb.CASCredentialsServiceGetRequest{ |
| 159 | + Digest: digestOrgWide, |
| 160 | + Role: pb.CASCredentialsServiceGetRequest_ROLE_DOWNLOADER, |
| 161 | + }) |
| 162 | + s.Require().Error(err) |
| 163 | + s.True(kerrors.IsForbidden(err), "expected forbidden, got %v", err) |
| 164 | + }) |
| 165 | + |
| 166 | + s.Run("project-scoped token can get downloader credentials for its own project's artifact", func() { |
| 167 | + resp, err := s.credentialsSvc.Get(s.ctxForToken(s.projectToken), &pb.CASCredentialsServiceGetRequest{ |
| 168 | + Digest: digestProjectA, |
| 169 | + Role: pb.CASCredentialsServiceGetRequest_ROLE_DOWNLOADER, |
| 170 | + }) |
| 171 | + s.Require().NoError(err) |
| 172 | + s.NotEmpty(resp.GetResult().GetToken()) |
| 173 | + }) |
| 174 | + |
| 175 | + s.Run("org-scoped token still reaches the whole organization", func() { |
| 176 | + resp, err := s.credentialsSvc.Get(s.ctxForToken(s.orgToken), &pb.CASCredentialsServiceGetRequest{ |
| 177 | + Digest: digestProjectB, |
| 178 | + Role: pb.CASCredentialsServiceGetRequest_ROLE_DOWNLOADER, |
| 179 | + }) |
| 180 | + s.Require().NoError(err) |
| 181 | + s.NotEmpty(resp.GetResult().GetToken()) |
| 182 | + }) |
| 183 | +} |
| 184 | + |
| 185 | +// ctxForToken builds a context equivalent to the one the authz middlewares produce for an |
| 186 | +// API-token authenticated call: current org, current API token and the api-token authz subject. |
| 187 | +func (s *casDownloadRBACIntegrationSuite) ctxForToken(token *biz.APIToken) context.Context { |
| 188 | + ctx := entities.WithCurrentOrg(context.Background(), &entities.Org{ID: s.org.ID, Name: s.org.Name}) |
| 189 | + ctx = entities.WithCurrentAPIToken(ctx, &entities.APIToken{ |
| 190 | + ID: token.ID.String(), |
| 191 | + Name: token.Name, |
| 192 | + ProjectID: token.ProjectID, |
| 193 | + }) |
| 194 | + |
| 195 | + return usercontext.WithAuthzSubject(ctx, (&authz.SubjectAPIToken{ID: token.ID.String()}).String()) |
| 196 | +} |
| 197 | + |
| 198 | +func (s *casDownloadRBACIntegrationSuite) writeECPrivateKey() string { |
| 199 | + // The CAS JWT builder signs with ES512, which requires a P-521 curve. |
| 200 | + key, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader) |
| 201 | + s.Require().NoError(err) |
| 202 | + |
| 203 | + der, err := x509.MarshalECPrivateKey(key) |
| 204 | + s.Require().NoError(err) |
| 205 | + |
| 206 | + path := filepath.Join(s.T().TempDir(), "cas-key.pem") |
| 207 | + s.Require().NoError(os.WriteFile(path, pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: der}), 0o600)) |
| 208 | + |
| 209 | + return path |
| 210 | +} |
| 211 | + |
| 212 | +func TestCASDownloadRBACIntegration(t *testing.T) { |
| 213 | + suite.Run(t, new(casDownloadRBACIntegrationSuite)) |
| 214 | +} |
0 commit comments