Skip to content

Commit 795ffc3

Browse files
committed
refactor(controlplane): make CAS source_internal resolution best-effort
Flagging a CAS token as internal platform traffic now happens only when a system API token explicitly requests it. Any other caller requesting it is silently not flagged instead of being rejected, so outdated systems whose tokens are not yet marked as system continue to work. Assisted-by: Claude Code Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev> Chainloop-Trace-Sessions: 8734066a-0121-4178-a08a-a59b8b8c8676
1 parent 6ed2859 commit 795ffc3

2 files changed

Lines changed: 16 additions & 32 deletions

File tree

app/controlplane/internal/service/cascredential.go

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,7 @@ func (s *CASCredentialsService) Get(ctx context.Context, req *pb.CASCredentialsS
5858
}
5959

6060
// Internal platform traffic can be flagged so the CAS skips audit event emission for it
61-
sourceInternal, err := resolveSourceInternal(req.GetSourceInternal(), currentAPIToken)
62-
if err != nil {
63-
return nil, err
64-
}
61+
sourceInternal := resolveSourceInternal(req.GetSourceInternal(), currentAPIToken)
6562

6663
currentOrg, err := requireCurrentOrg(ctx)
6764
if err != nil {
@@ -167,17 +164,12 @@ func (s *CASCredentialsService) Get(ctx context.Context, req *pb.CASCredentialsS
167164
}, nil
168165
}
169166

170-
// resolveSourceInternal returns whether the minted CAS token must be flagged as internal
171-
// platform traffic. Only system API tokens can request it since they are minted exclusively
172-
// by internal code paths; any other caller asking for it is rejected.
173-
func resolveSourceInternal(requested bool, token *entities.APIToken) (bool, error) {
174-
if !requested {
175-
return false, nil
176-
}
177-
178-
if token == nil || !token.IsSystem {
179-
return false, errors.Forbidden("forbidden", "source_internal is restricted to system API tokens")
180-
}
181-
182-
return true, nil
167+
// resolveSourceInternal returns whether the minted CAS token should be flagged as internal
168+
// platform traffic. It is only flagged when a system API token explicitly requests it, since
169+
// these tokens are minted exclusively by internal code paths.
170+
//
171+
// This is best-effort: any other caller requesting it (for example an outdated system whose
172+
// token store does not yet mark its tokens as system) is simply not flagged rather than rejected.
173+
func resolveSourceInternal(requested bool, token *entities.APIToken) bool {
174+
return requested && token != nil && token.IsSystem
183175
}

app/controlplane/internal/service/cascredential_test.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ import (
1919
"testing"
2020

2121
"github.com/chainloop-dev/chainloop/app/controlplane/internal/usercontext/entities"
22-
"github.com/go-kratos/kratos/v2/errors"
2322
"github.com/stretchr/testify/assert"
24-
"github.com/stretchr/testify/require"
2523
)
2624

2725
func TestResolveSourceInternal(t *testing.T) {
@@ -30,7 +28,6 @@ func TestResolveSourceInternal(t *testing.T) {
3028
requested bool
3129
token *entities.APIToken
3230
want bool
33-
wantErr bool
3431
}{
3532
{
3633
name: "not requested, no token (user auth)",
@@ -57,29 +54,24 @@ func TestResolveSourceInternal(t *testing.T) {
5754
want: true,
5855
},
5956
{
60-
name: "requested by regular API token is forbidden",
57+
// best-effort: a non-system token requesting it is ignored, not rejected,
58+
// to tolerate outdated systems whose tokens are not yet marked as system
59+
name: "requested by regular API token falls back to false",
6160
requested: true,
6261
token: &entities.APIToken{},
63-
wantErr: true,
62+
want: false,
6463
},
6564
{
66-
name: "requested by user auth is forbidden",
65+
name: "requested by user auth falls back to false",
6766
requested: true,
6867
token: nil,
69-
wantErr: true,
68+
want: false,
7069
},
7170
}
7271

7372
for _, tc := range testCases {
7473
t.Run(tc.name, func(t *testing.T) {
75-
got, err := resolveSourceInternal(tc.requested, tc.token)
76-
if tc.wantErr {
77-
require.Error(t, err)
78-
assert.True(t, errors.IsForbidden(err))
79-
return
80-
}
81-
82-
require.NoError(t, err)
74+
got := resolveSourceInternal(tc.requested, tc.token)
8375
assert.Equal(t, tc.want, got)
8476
})
8577
}

0 commit comments

Comments
 (0)