Skip to content
Open
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
20 changes: 12 additions & 8 deletions cmd/git/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ import (
"context"
"os"

atmosgit "github.com/cloudposse/atmos/pkg/git"
"github.com/spf13/viper"

"github.com/cloudposse/atmos/pkg/auth/gitenv"
cfg "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/perf"
)

// composeEnv builds the subprocess environment for a Git operation.
// When identity is non-empty, the identity environment (GIT_CONFIG_* from
// GitHub STS etc.) is merged over the current process environment via
// atmosgit.ComposeEnvironment. When identity is empty, the process environment
// is returned unchanged (ambient credentials from the developer's Git config
// or GITHUB_TOKEN apply). Auth manager injection is a planned future enhancement.
// envComposer builds Git subprocess environments from Atmos Auth identities. Tests replace it.
var envComposer = gitenv.NewComposer()

// composeEnv builds the subprocess environment for a Git operation: the process environment plus
// the environment of the effective identity. The global --identity flag (or ATMOS_IDENTITY) wins
// over the repository's configured identity; with neither, the process environment is returned
// unchanged (ambient credentials from the developer's Git config or GITHUB_TOKEN apply).
func composeEnv(ctx context.Context, identity string) ([]string, error) {
defer perf.Track(nil, "git.composeEnv")()

return atmosgit.ComposeEnvironment(ctx, os.Environ(), identity, nil)
return envComposer.Compose(ctx, atmosConfigPtr, os.Environ(), viper.GetString(cfg.IdentityFlagName), identity)
}
45 changes: 45 additions & 0 deletions cmd/git/orchestration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import (
"context"
"errors"
"os"
"strings"
"testing"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"

errUtils "github.com/cloudposse/atmos/errors"
"github.com/cloudposse/atmos/pkg/auth/gitenv"
"github.com/cloudposse/atmos/pkg/ci"
atmosgit "github.com/cloudposse/atmos/pkg/git"
"github.com/cloudposse/atmos/pkg/schema"
Expand Down Expand Up @@ -576,6 +579,48 @@ func TestComposeEnv_EmptyIdentity(t *testing.T) {
assert.NotEmpty(t, env)
}

// ---- composeEnv identity precedence ----

func TestComposeEnv_IdentityReachesSubprocessEnvironment(t *testing.T) {
original := envComposer
t.Cleanup(func() { envComposer = original })
manager := gitenv.NewMockIdentityManager(gomock.NewController(t))
// Each identity is authenticated only by the case that selects it: the mock fails the test if
// the repository identity is resolved when the flag overrides it, or if any is with neither.
manager.EXPECT().EnsureIdentityEnvironment(gomock.Any(), "repo-id").Return(map[string]string{"ATMOS_TEST_GIT_SOCK": "repo"}, nil).Times(1)
manager.EXPECT().EnsureIdentityEnvironment(gomock.Any(), "flag-id").Return(map[string]string{"ATMOS_TEST_GIT_SOCK": "flag"}, nil).Times(1)
envComposer = gitenv.NewComposer(gitenv.WithManagerFactory(func(*schema.AtmosConfiguration) (gitenv.IdentityManager, error) {
return manager, nil
}))
setAtmosConfigPtr(t, &schema.AtmosConfiguration{})
t.Setenv("ATMOS_TEST_GIT_SOCK", "ambient")

tests := []struct {
name, flag, repo string
want string
}{
{"no identity keeps the process environment", "", "", "ambient"},
{"repository identity", "", "repo-id", "repo"},
{"flag overrides repository identity", "flag-id", "repo-id", "flag"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
viper.Set("identity", tt.flag)
t.Cleanup(func() { viper.Set("identity", "") })

env, err := composeEnv(context.Background(), tt.repo)
require.NoError(t, err)
var got []string
for _, entry := range env {
if strings.HasPrefix(entry, "ATMOS_TEST_GIT_SOCK=") {
got = append(got, strings.TrimPrefix(entry, "ATMOS_TEST_GIT_SOCK="))
}
}
assert.Equal(t, []string{tt.want}, got)
})
}
}

func TestCloneRefExcludesBranchAndOverridesConfiguredBranch(t *testing.T) {
opts := &cloneOptions{Ref: "revision", Branch: "main"}
require.ErrorIs(t, runClone(context.Background(), opts, []string{"https://example.com/repo.git"}), errUtils.ErrInvalidConfig)
Expand Down
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ import (
// Import the Atmos Pro credential broker so it registers itself (init) and is consulted
// before the first remote read in CI (lazily provisions the github/sts integration).
_ "github.com/cloudposse/atmos/pkg/auth/providers/atmospro/broker"
// Import the remote-read identity resolver so `identity:` on vendored sources, component sources,
// and remote imports resolves through Atmos Auth (registers itself with the downloader in init).
_ "github.com/cloudposse/atmos/pkg/auth/remoteread"
"github.com/cloudposse/atmos/pkg/ci"
cistartup "github.com/cloudposse/atmos/pkg/ci/startup"
// Import component providers to register them with the component registry.
Expand Down
47 changes: 47 additions & 0 deletions docs/fixes/2026-11-12-remote-read-identity-environment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Fix: identity environments now reach `atmos git`, `atmos ansible playbook`, and stored plugin credentials

**Date:** 2026-11-12

## Summary

Three places that should have used an Atmos Auth identity did not. `atmos git` ignored
`git.repositories.<name>.auth.identity` and the `--identity` flag. `atmos ansible playbook --identity`
authenticated but never gave the identity's environment to `ansible-playbook`. A plugin credential of
kind `environment/v1` that was stored by an earlier process failed in a fresh process.

## Context

These surfaced while adding `identity:` to vendored sources, component sources, and remote stack
imports, which all depend on an identity's environment reaching the subprocess that does the work.

- `atmos git` built each Git subprocess environment from the process environment only, so a configured
repository identity (for example a `github/sts`-linked identity) and `--identity` had no effect.
- The Ansible executor processed the stack with the identity but then built the playbook environment from
the component environment alone. Helmfile and Packer already layered the identity's environment on top.
- After `atmos auth login`, a later process finds the stored credential still valid and skips
authentication. The `environment/v1` materializer was only populated by authentication, so in that fresh
process (`atmos auth exec`, `atmos auth shell`, `atmos git`) it held no environment and the command failed.

## Changes

- `atmos git` composes the Git subprocess environment from the process environment plus the effective
identity. The global `--identity` flag (or `ATMOS_IDENTITY`) wins over the repository's configured
identity. With neither, the process environment is used unchanged.
- `atmos ansible playbook` layers the identity's prepared environment over the component environment before
starting `ansible-playbook`, the same way Helmfile and Packer do. Its debug log lists the names of the
environment variables the playbook runs with, not their values, because that environment now carries the
identity's credentials.
- The `environment/v1` materializer can now be restored from a credential read back from the credential
store. It applies the same validation and secret masking as a fresh authentication, and refuses an
expired or malformed credential.

## Validation

- Unit tests cover the identity environment reaching the `atmos git` subprocess, the Ansible playbook
environment with an auth manager, without one, and when preparation fails, and restoring a stored
`environment/v1` credential, including refusal of an expired one.
- The website build covers the documentation for these behaviors.

## Follow-ups

None.
Loading
Loading