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
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,24 +239,17 @@ We refer to each method of providing credentials as "credential providers" (like
The CLI supports the following authentication methods via the environment:

- [Spacelift API tokens](#spacelift-api-tokens).
- [GitHub tokens](#github-tokens).
- [Spacelift API keys](#spacelift-api-keys).
- [GitHub tokens](#github-tokens).

`spacectl` looks for authentication configurations in the order specified above, and will stop as soon as it finds a valid configuration. For example, if a Spacelift API token is specified, GitHub tokens and Spacelift API keys will be ignored, even if their environment variables are specified.
`spacectl` looks for authentication configurations in the order specified above, and will stop as soon as it finds a valid configuration. For example, if a Spacelift API token is specified, Spacelift API keys and GitHub tokens will be ignored, even if their environment variables are specified.

#### Spacelift API tokens

Spacelift API tokens can be specified using the `SPACELIFT_API_TOKEN` environment variable. When this variable is found, the CLI ignores all the other authentication environment variables because the token contains all the information needed to authenticate.

NOTE: API tokens are generally short-lived and will need to be re-created often.

#### GitHub tokens

GitHub tokens are only available to accounts that use GitHub as their identity provider, but are very convenient for use in GitHub actions. To use a GitHub token, set the following environment variables:

- `SPACELIFT_API_KEY_ENDPOINT` - the URL to your Spacelift account, for example `https://mycorp.app.spacelift.io`.
- `SPACELIFT_API_GITHUB_TOKEN` - a GitHub personal access token.

#### Spacelift API keys

To use a Spacelift API key, set the following environment variables:
Expand All @@ -265,6 +258,13 @@ To use a Spacelift API key, set the following environment variables:
- `SPACELIFT_API_KEY_ID` - the ID of your Spacelift API key. Available via the Spacelift application.
- `SPACELIFT_API_KEY_SECRET` - the secret for your API key. Only available when the secret is created.

#### GitHub tokens

GitHub tokens are only available to accounts that use GitHub as their identity provider. To use a GitHub token, set the following environment variables:

- `SPACELIFT_API_KEY_ENDPOINT` - the URL to your Spacelift account, for example `https://mycorp.app.spacelift.io`.
- `SPACELIFT_API_GITHUB_TOKEN` - a GitHub personal access token.

More information about API authentication can be found at <https://docs.spacelift.io/integrations/api#authenticating-with-the-api>.

### Authenticating using account profiles
Expand Down
12 changes: 8 additions & 4 deletions client/session/from_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package session

import (
"context"
stderrors "errors"
"fmt"
"net/http"
"os"
Expand Down Expand Up @@ -69,17 +70,20 @@ func FromEnvironment(ctx context.Context, client *http.Client) func(func(string)
return tryAuthMethod(ctx, client, preferredMethod, lookup)
}

var lastErr error
for _, method := range []string{authMethodToken, authMethodGitHub, authMethodAPIKey} {
// All failures are reported: the method the caller meant to use is
// indistinguishable from the ones they left unset, so surfacing a
// single reason would often be the wrong one.
var errs []error
for _, method := range []string{authMethodToken, authMethodAPIKey, authMethodGitHub} {
session, err := tryAuthMethod(ctx, client, method, lookup)
if err != nil {
lastErr = err
errs = append(errs, err)
}
if session != nil {
return session, nil
}
}
return nil, lastErr
return nil, stderrors.Join(errs...)
}
}

Expand Down
7 changes: 4 additions & 3 deletions client/session/from_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package session

import (
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -42,7 +43,7 @@ func TestFromEnvironment(t *testing.T) {
l := lookupWithEnv("", "", "abc123", "SuperSecret")
g.It("expect an error to find api endpoint in environment", func() {
_, err := FromEnvironment(context.TODO(), nil)(l)
g.Assert(err).Equal(errEnvSpaceliftAPIKeyEndpoint)
g.Assert(errors.Is(err, errEnvSpaceliftAPIKeyEndpoint)).IsTrue("expected the api endpoint error to be reported")
})
})

Expand Down Expand Up @@ -88,15 +89,15 @@ func TestFromEnvironment(t *testing.T) {
g.It("expect an error to find api key id in environment", func() {
l := lookupWithEnv("https://spacectl.app.spacelift.io", "", "", "SuperSecret")
_, err := FromEnvironment(context.TODO(), nil)(l)
g.Assert(err).Equal(errEnvSpaceliftAPIKeyID)
g.Assert(errors.Is(err, errEnvSpaceliftAPIKeyID)).IsTrue("expected the api key id error to be reported")
})
})

g.Describe("EnvSpaceliftAPIKeyID is set, EnvSpaceliftAPIKeySecret is an empty string", func() {
g.It("expect an error to find api key secret in environment", func() {
l := lookupWithEnv("https://spacectl.app.spacelift.io", "", "abc123", "")
_, err := FromEnvironment(context.TODO(), nil)(l)
g.Assert(err).Equal(errEnvSpaceliftAPIKeySecret)
g.Assert(errors.Is(err, errEnvSpaceliftAPIKeySecret)).IsTrue("expected the api key secret error to be reported")
})
})
})
Expand Down
Loading