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
2 changes: 1 addition & 1 deletion build/package/purls.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pkg:golang/github.com/mholt/[email protected]
pkg:golang/github.com/mikelolasagasti/[email protected]
pkg:golang/github.com/minio/[email protected]
pkg:golang/github.com/mongodb-forks/[email protected]
pkg:golang/github.com/mongodb/[email protected]20250822132614-230a610dbe0f
pkg:golang/github.com/mongodb/[email protected]20250901162552-2e62c5010f93
pkg:golang/github.com/montanaflynn/[email protected]
pkg:golang/github.com/nwaples/rardecode/[email protected]
pkg:golang/github.com/pelletier/go-toml/[email protected]
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ require (
github.com/mattn/go-isatty v0.0.20
github.com/mholt/archives v0.1.3
github.com/mongodb-labs/cobra2snooty v1.19.1
github.com/mongodb/atlas-cli-core v0.0.0-20250822132614-230a610dbe0f
github.com/mongodb/atlas-cli-core v0.0.0-20250901162552-2e62c5010f93
github.com/pelletier/go-toml v1.9.5
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
github.com/shirou/gopsutil/v4 v4.25.7
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,8 @@ github.com/mongodb-forks/digest v1.1.0 h1:7eUdsR1BtqLv0mdNm4OXs6ddWvR4X2/OsLwdKk
github.com/mongodb-forks/digest v1.1.0/go.mod h1:rb+EX8zotClD5Dj4NdgxnJXG9nwrlx3NWKJ8xttz1Dg=
github.com/mongodb-labs/cobra2snooty v1.19.1 h1:GDEQZWy8f/DeJlImNgVvStu6sgNi8nuSOR1Oskcw8BI=
github.com/mongodb-labs/cobra2snooty v1.19.1/go.mod h1:Hyq4YadN8dwdOiz56MXwTuVN63p0WlkQwxdLxOSGdX8=
github.com/mongodb/atlas-cli-core v0.0.0-20250822132614-230a610dbe0f h1:oSNF4zFkefc61U5K/C/rT6A0tsbu7cG/Tdo1ZVKbUGA=
github.com/mongodb/atlas-cli-core v0.0.0-20250822132614-230a610dbe0f/go.mod h1:QDfVGpdfxXM1httLNXCKsfWTKv6slzCqBZxkkPIktlQ=
github.com/mongodb/atlas-cli-core v0.0.0-20250901162552-2e62c5010f93 h1:arSkz/kAB+5jgo0efH24UByHsYe7QZ9cHgnAXZPRPeg=
github.com/mongodb/atlas-cli-core v0.0.0-20250901162552-2e62c5010f93/go.mod h1:QDfVGpdfxXM1httLNXCKsfWTKv6slzCqBZxkkPIktlQ=
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
github.com/nwaples/rardecode/v2 v2.1.0 h1:JQl9ZoBPDy+nIZGb1mx8+anfHp/LV3NE2MjMiv0ct/U=
Expand Down
4 changes: 2 additions & 2 deletions internal/api/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/mongodb/atlas-cli-core/config"
"github.com/mongodb/atlas-cli-core/transport"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/log"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/version"
)

var (
Expand Down Expand Up @@ -72,7 +72,7 @@ func NewExecutor(commandConverter CommandConverter, httpClient Doer, formatter R
func NewDefaultExecutor(formatter ResponseFormatter) (*Executor, error) {
profile := config.Default()

client, err := store.HTTPClient(profile, transport.Default())
client, err := transport.HTTPClient(version.Version, transport.Default())
if err != nil {
return nil, err
}
Expand Down
34 changes: 3 additions & 31 deletions internal/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,34 +53,6 @@ type Store struct {
ctx context.Context
}

func HTTPClient(c CredentialsGetter, httpTransport http.RoundTripper) (*http.Client, error) {
switch c.AuthType() {
case config.APIKeys:
t := transport.NewDigestTransport(c.PublicAPIKey(), c.PrivateAPIKey(), httpTransport)
return t.Client()
case config.UserAccount:
token, err := c.Token()
if err != nil {
return nil, err
}
tr, err := transport.NewAccessTokenTransport(token, httpTransport, version.Version, func(t *atlasauth.Token) error {
config.SetAccessToken(t.AccessToken)
config.SetRefreshToken(t.RefreshToken)
return config.Save()
})
if err != nil {
return nil, err
}
return &http.Client{Transport: tr}, nil
case config.ServiceAccount:
return transport.NewServiceAccountClientWithHost(c.ClientID(), c.ClientSecret(), config.OpsManagerURL()), nil
case config.NoAuth:
fallthrough
default:
return &http.Client{Transport: httpTransport}, nil
}
}

func (s *Store) transport() *http.Transport {
switch {
case s.telemetry:
Expand Down Expand Up @@ -144,9 +116,9 @@ type CredentialsGetter interface {
}

// WithAuthentication sets the store credentials.
func WithAuthentication(c CredentialsGetter) Option {
func WithAuthentication() Option {
return func(s *Store) error {
client, err := HTTPClient(c, s.transport())
client, err := transport.HTTPClient(version.Version, s.transport())
if err != nil {
return err
}
Expand Down Expand Up @@ -256,7 +228,7 @@ type AuthenticatedConfig interface {

// AuthenticatedPreset is the default Option when connecting to the public API with authentication.
func AuthenticatedPreset(c AuthenticatedConfig) Option {
options := []Option{Service(c.Service()), WithAuthentication(c)}
options := []Option{Service(c.Service()), WithAuthentication()}
if baseURLOpt := baseURLOption(c); baseURLOpt != nil {
options = append(options, baseURLOpt)
}
Expand Down
34 changes: 19 additions & 15 deletions internal/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,38 +118,42 @@ var _ AuthenticatedConfig = &testConfig{}

func TestWithAuthentication(t *testing.T) {
tests := []struct {
name string
a auth
name string
setTestProfile func(p *config.Profile)
}{
{
name: "api keys",
a: auth{
username: "username",
password: "password",
setTestProfile: func(p *config.Profile) {
p.SetAuthType(config.APIKeys)
p.SetPublicAPIKey("test-key")
p.SetPrivateAPIKey("test-secret")
},
},
{
name: "service account",
a: auth{
clientID: "id",
clientSecret: "secret",
setTestProfile: func(p *config.Profile) {
p.SetAuthType(config.ServiceAccount)
p.SetClientID("id")
p.SetClientSecret("secret")
},
},
{
name: "user account",
a: auth{
refreshToken: "token",
accessToken: &atlasauth.Token{
AccessToken: "access",
RefreshToken: "refresh",
},
setTestProfile: func(p *config.Profile) {
p.SetAuthType(config.UserAccount)
p.SetAccessToken("token")
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c, err := New(Service("cloud"), WithAuthentication(tt.a))
// Set up profile for testing
profile := config.NewProfile("test", config.NewInMemoryStore())
tt.setTestProfile(profile)
config.SetDefaultProfile(profile)

c, err := New(Service("cloud"), WithAuthentication())
require.NoError(t, err)
require.NotNil(t, c.httpClient)
require.NotNil(t, c.httpClient.Transport)
Expand Down
Loading