Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ linters:
rules:
- path: _test\.go
text: Potential hardcoded credentials
# Env var names in runner definitions are data, not magic strings
- path: pkg/attestation/crafter/runners/
linters:
- goconst
- linters:
- staticcheck
text: 'SA1019.*crafting_schema\.proto'
Expand Down
7 changes: 7 additions & 0 deletions app/cli/cmd/organization_describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ func contextTableOutput(config *action.ConfigContextItem) error {
orgInfo += "\nBlock attestations on released versions: enabled"
}

if r := m.Org.EnvVarRedaction; r != nil {
orgInfo += fmt.Sprintf("\nEnv var redaction: %s", r.Mode)
if len(r.EnvVars) > 0 {
orgInfo += fmt.Sprintf(" (%s)", strings.Join(r.EnvVars, ", "))
}
}

gt.AppendRow(table.Row{"Organization", orgInfo})
}

Expand Down
18 changes: 18 additions & 0 deletions app/cli/cmd/organization_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ package cmd
import (
"fmt"
"strconv"
"strings"

"github.com/chainloop-dev/chainloop/app/cli/pkg/action"
"github.com/chainloop-dev/chainloop/pkg/attestation/crafter/runners"
"github.com/chainloop-dev/chainloop/pkg/attestation/envredaction"
"github.com/spf13/cobra"
)

Expand All @@ -34,6 +37,8 @@ func newOrganizationUpdateCmd() *cobra.Command {
enableAIAgentCollector bool
blockAttestationsOnReleasedVersions bool
skipRunnerEnvVars bool
envVarRedaction string
redactedEnvVars []string
)

cmd := &cobra.Command{
Expand Down Expand Up @@ -69,6 +74,14 @@ func newOrganizationUpdateCmd() *cobra.Command {
opts.SkipRunnerEnvVars = &skipRunnerEnvVars
}

if cmd.Flags().Changed("env-var-redaction") || cmd.Flags().Changed("redacted-env-var") {
redaction, err := action.NewEnvVarRedactionPb(envVarRedaction, redactedEnvVars)
if err != nil {
return err
}
opts.EnvVarRedaction = redaction
}

if cmd.Flags().Changed("api-token-max-days-inactive") {
days, err := strconv.Atoi(apiTokenMaxDaysInactive)
if err != nil {
Expand Down Expand Up @@ -102,5 +115,10 @@ func newOrganizationUpdateCmd() *cobra.Command {
cmd.Flags().BoolVar(&enableAIAgentCollector, "enable-ai-agent-collector", false, "enable automatic AI agent config collection during attestation init")
cmd.Flags().BoolVar(&blockAttestationsOnReleasedVersions, "block-attestations-on-released-versions", false, "reject new attestations pushed to project versions that are already released")
cmd.Flags().BoolVar(&skipRunnerEnvVars, "skip-runner-env-vars", false, "opt out of storing the environment variables automatically discovered by the CI runner in the attestation")
cmd.Flags().StringVar(&envVarRedaction, "env-var-redaction", "", fmt.Sprintf(
"replace the values of environment variables with %s in new attestations, one of: %s, %s (variables that identify a person: %s), %s (the variables in --redacted-env-var)",
envredaction.RedactedValue, envredaction.ModeNone, envredaction.ModeKnownIdentity,
strings.Join(runners.KnownIdentityEnvVars(), ", "), envredaction.ModeCustom))
cmd.Flags().StringArrayVar(&redactedEnvVars, "redacted-env-var", nil, "exact name of an environment variable to redact, can be set multiple times. Requires --env-var-redaction custom")
return cmd
}
2 changes: 2 additions & 0 deletions app/cli/documentation/cli-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2774,10 +2774,12 @@ Options
--block[=true|false] set the default policy violation blocking strategy
--block-attestations-on-released-versions[=true|false] reject new attestations pushed to project versions that are already released
--enable-ai-agent-collector[=true|false] enable automatic AI agent config collection during attestation init
--env-var-redaction string replace the values of environment variables with [REDACTED] in new attestations, one of: none, known-identity (variables that identify a person: BUILD_REQUESTEDFOR, BUILD_REQUESTEDFOREMAIL, GITHUB_ACTOR, GITLAB_USER_EMAIL, GITLAB_USER_LOGIN, HOME, USER), custom (the variables in --redacted-env-var)
-h, --help[=true|false] help for update
--name string organization name
--policies-allowed-hostnames strings set the allowed hostnames for the policy engine
--prevent-implicit-workflow-creation[=true|false] prevent workflows and projects from being created implicitly during attestation init
--redacted-env-var stringArray exact name of an environment variable to redact, can be set multiple times. Requires --env-var-redaction custom
--restrict-contract-creation[=true|false] restrict contract creation (org-level and project-level) to only organization admins (owner/admin roles)
--skip-runner-env-vars[=true|false] opt out of storing the environment variables automatically discovered by the CI runner in the attestation
```
Expand Down
4 changes: 4 additions & 0 deletions app/cli/pkg/action/attestation_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ func (action *AttestationInit) Run(ctx context.Context, opts *AttestationInitRun
policiesAllowedHostnames []string
enableAIAgentCollector bool
skipRunnerEnvVars bool
redactedEnvVars []string
// Timestamp Authority URL for new attestations
timestampAuthorityURL, signingCAName string
uiDashboardURL string
Expand Down Expand Up @@ -266,6 +267,8 @@ func (action *AttestationInit) Run(ctx context.Context, opts *AttestationInitRun
policiesAllowedHostnames = result.GetPoliciesAllowedHostnames()
enableAIAgentCollector = result.GetEnableAiAgentCollector()
skipRunnerEnvVars = result.GetSkipRunnerEnvVars()
// the control plane resolves the names for every mode, so they match its push check
redactedEnvVars = result.GetRedactedEnvVars()
signingOpts := result.GetSigningOptions()
timestampAuthorityURL = signingOpts.GetTimestampAuthorityUrl()
signingCAName = signingOpts.GetSigningCa()
Expand Down Expand Up @@ -319,6 +322,7 @@ func (action *AttestationInit) Run(ctx context.Context, opts *AttestationInitRun
BlockOnPolicyViolation: blockOnPolicyViolation,
PoliciesAllowedHostnames: policiesAllowedHostnames,
SkipRunnerEnvVars: skipRunnerEnvVars,
RedactedEnvVars: redactedEnvVars,
SigningOptions: &crafter.SigningOpts{
TimestampAuthorityURL: timestampAuthorityURL,
SigningCAName: signingCAName,
Expand Down
64 changes: 64 additions & 0 deletions app/cli/pkg/action/env_var_redaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// Copyright 2026 The Chainloop Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package action

import (
"errors"

pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1"
"github.com/chainloop-dev/chainloop/pkg/attestation/envredaction"
)

// EnvVarRedactionItem is the org-level redaction of env var values in attestations.
type EnvVarRedactionItem struct {
Mode envredaction.Mode `json:"mode"`
EnvVars []string `json:"envVars,omitempty"`
}

// NewEnvVarRedactionPb builds the API setting from the CLI flag values.
func NewEnvVarRedactionPb(mode string, envVars []string) (*pb.EnvVarRedaction, error) {
m := envredaction.Mode(mode)
if len(envVars) > 0 && m != envredaction.ModeCustom {
return nil, errors.New("redacted env vars can only be set with the custom mode")
}

// Names rejects an unknown mode and a custom mode with no names, as the control plane does.
if _, err := envredaction.Names(m, envVars); err != nil {
return nil, err
}

out := &pb.EnvVarRedaction{Type: pb.EnvVarRedaction_TYPE_UNSPECIFIED, EnvVars: envVars}
switch m {
case envredaction.ModeKnownIdentity:
out.Type = pb.EnvVarRedaction_TYPE_KNOWN_IDENTITY
case envredaction.ModeCustom:
out.Type = pb.EnvVarRedaction_TYPE_CUSTOM
}

return out, nil
}

// pbEnvVarRedactionToAction converts the API setting for output. No redaction returns nil.
func pbEnvVarRedactionToAction(in *pb.EnvVarRedaction) *EnvVarRedactionItem {
switch in.GetType() {
case pb.EnvVarRedaction_TYPE_KNOWN_IDENTITY:
return &EnvVarRedactionItem{Mode: envredaction.ModeKnownIdentity, EnvVars: in.GetEnvVars()}
case pb.EnvVarRedaction_TYPE_CUSTOM:
return &EnvVarRedactionItem{Mode: envredaction.ModeCustom, EnvVars: in.GetEnvVars()}
default:
return nil
Comment thread
migmartri marked this conversation as resolved.
Resolved
}
}
87 changes: 87 additions & 0 deletions app/cli/pkg/action/env_var_redaction_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//
// Copyright 2026 The Chainloop Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package action

import (
"testing"

pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1"
"github.com/chainloop-dev/chainloop/pkg/attestation/envredaction"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
)

func TestNewEnvVarRedactionItem(t *testing.T) {
testCases := []struct {
name string
mode string
vars []string
want *pb.EnvVarRedaction
wantErr string
}{
{name: "none", mode: string(envredaction.ModeNone), want: &pb.EnvVarRedaction{}},
{name: "known identity", mode: string(envredaction.ModeKnownIdentity), want: &pb.EnvVarRedaction{Type: pb.EnvVarRedaction_TYPE_KNOWN_IDENTITY}},
{
name: "custom list",
mode: string(envredaction.ModeCustom),
vars: []string{"BUILD_REQUESTEDFOR", "BUILD_REQUESTEDFOREMAIL"},
want: &pb.EnvVarRedaction{Type: pb.EnvVarRedaction_TYPE_CUSTOM, EnvVars: []string{"BUILD_REQUESTEDFOR", "BUILD_REQUESTEDFOREMAIL"}},
},
{name: "custom without variables", mode: string(envredaction.ModeCustom), wantErr: "custom env var redaction requires at least one variable name"},
{name: "variables without custom", mode: string(envredaction.ModeKnownIdentity), vars: []string{"MY_OWNER"}, wantErr: "redacted env vars can only be set with the custom mode"},
{name: "unknown mode", mode: "unknown-mode", wantErr: "invalid env var redaction mode \"unknown-mode\", valid values are: none, known-identity, custom"},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got, err := NewEnvVarRedactionPb(tc.mode, tc.vars)
if tc.wantErr != "" {
require.EqualError(t, err, tc.wantErr)
return
}
require.NoError(t, err)
assert.True(t, proto.Equal(tc.want, got), "got %v, want %v", got, tc.want)
})
}
}

func TestEnvVarRedactionConversions(t *testing.T) {
testCases := []struct {
name string
in *pb.EnvVarRedaction
wantItem *EnvVarRedactionItem
}{
{name: "not set", in: nil, wantItem: nil},
{name: "unspecified", in: &pb.EnvVarRedaction{}, wantItem: nil},
{
name: "known identity",
in: &pb.EnvVarRedaction{Type: pb.EnvVarRedaction_TYPE_KNOWN_IDENTITY},
wantItem: &EnvVarRedactionItem{Mode: envredaction.ModeKnownIdentity},
},
{
name: "custom list item",
in: &pb.EnvVarRedaction{Type: pb.EnvVarRedaction_TYPE_CUSTOM, EnvVars: []string{"OTHER_VAR"}},
wantItem: &EnvVarRedactionItem{Mode: envredaction.ModeCustom, EnvVars: []string{"OTHER_VAR"}},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.wantItem, pbEnvVarRedactionToAction(tc.in))
})
}
}
22 changes: 12 additions & 10 deletions app/cli/pkg/action/membership_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@ type MembershipList struct {
}

type OrgItem struct {
ID string `json:"id"`
Name string `json:"name"`
CreatedAt *time.Time `json:"createdAt"`
PolicyViolationBlockingStrategy string `json:"policyViolationBlockingStrategy"`
PolicyAllowedHostnames []string `json:"policyAllowedHostnames,omitempty"`
PreventImplicitWorkflowCreation bool `json:"preventImplicitWorkflowCreation"`
APITokenMaxDaysInactive *string `json:"apiTokenMaxDaysInactive,omitempty"`
EnableAIAgentCollector bool `json:"enableAiAgentCollector"`
BlockAttestationsOnReleasedVersions bool `json:"blockAttestationsOnReleasedVersions"`
SkipRunnerEnvVars bool `json:"skipRunnerEnvVars"`
ID string `json:"id"`
Name string `json:"name"`
CreatedAt *time.Time `json:"createdAt"`
PolicyViolationBlockingStrategy string `json:"policyViolationBlockingStrategy"`
PolicyAllowedHostnames []string `json:"policyAllowedHostnames,omitempty"`
PreventImplicitWorkflowCreation bool `json:"preventImplicitWorkflowCreation"`
APITokenMaxDaysInactive *string `json:"apiTokenMaxDaysInactive,omitempty"`
EnableAIAgentCollector bool `json:"enableAiAgentCollector"`
BlockAttestationsOnReleasedVersions bool `json:"blockAttestationsOnReleasedVersions"`
SkipRunnerEnvVars bool `json:"skipRunnerEnvVars"`
EnvVarRedaction *EnvVarRedactionItem `json:"envVarRedaction,omitempty"`
}

type MembershipItem struct {
Expand Down Expand Up @@ -144,6 +145,7 @@ func pbOrgItemToAction(in *pb.OrgItem) *OrgItem {
EnableAIAgentCollector: in.EnableAiAgentCollector,
BlockAttestationsOnReleasedVersions: in.BlockAttestationsOnReleasedVersions,
SkipRunnerEnvVars: in.SkipRunnerEnvVars,
EnvVarRedaction: pbEnvVarRedactionToAction(in.GetEnvVarRedaction()),
}

if in.DefaultPolicyViolationStrategy == pb.OrgItem_POLICY_VIOLATION_BLOCKING_STRATEGY_BLOCK {
Expand Down
3 changes: 3 additions & 0 deletions app/cli/pkg/action/org_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type NewOrgUpdateOpts struct {
BlockAttestationsOnReleasedVersions *bool
// SkipRunnerEnvVars opts out of storing the environment variables automatically discovered by the CI runner in the attestation
SkipRunnerEnvVars *bool
// EnvVarRedaction sets the redaction of env var values in new attestations. nil means no change
EnvVarRedaction *pb.EnvVarRedaction
}

func (action *OrgUpdate) Run(ctx context.Context, name string, opts *NewOrgUpdateOpts) (*OrgItem, error) {
Expand All @@ -57,6 +59,7 @@ func (action *OrgUpdate) Run(ctx context.Context, name string, opts *NewOrgUpdat
EnableAiAgentCollector: opts.EnableAIAgentCollector,
BlockAttestationsOnReleasedVersions: opts.BlockAttestationsOnReleasedVersions,
SkipRunnerEnvVars: opts.SkipRunnerEnvVars,
EnvVarRedaction: opts.EnvVarRedaction,
}

if opts.PoliciesAllowedHostnames != nil {
Expand Down
Loading
Loading