Skip to content

Commit

Permalink
Fix to use postgres as the controller credential store (#1016)
Browse files Browse the repository at this point in the history
* Fix to use postgres as the controller credential store

* Slight refactor

* Added and updated tests

* Allow jimm to continue start without a credential store

Jimm could error out if no credential store is setup but this would require several tests to change and is best left for a separate PR

* Updated test
  • Loading branch information
kian99 committed Aug 2, 2023
1 parent 4c42038 commit be4267f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 14 deletions.
5 changes: 5 additions & 0 deletions internal/jujuclient/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ func (d *Dialer) Dial(ctx context.Context, ctl *dbmodel.Controller, modelTag nam
}
}

if username == "" || password == "" {
zapctx.Error(ctx, "empty username or password")
return nil, errors.E(op, errors.CodeNotFound, "missing controller username or password")
}

args := jujuparams.LoginRequest{
AuthTag: names.NewUserTag(username).String(),
Credentials: password,
Expand Down
39 changes: 26 additions & 13 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,23 +292,13 @@ func NewService(ctx context.Context, p Params) (*Service, error) {
if err != nil {
return nil, errors.E(op, err)
}
vs, err := newVaultStore(ctx, p)
if err != nil {
zapctx.Error(ctx, "Vault Store error", zap.Error(err))

if err := s.setupCredentialStore(ctx, p); err != nil {
return nil, errors.E(op, err)
}
if vs != nil {
s.jimm.CredentialStore = vs
} else {
// Only enable Postgres storage for secrets if explictly enabled.
if _, ok := os.LookupEnv("INSECURE_SECRET_STORAGE"); ok {
zapctx.Warn(ctx, "using plaintext postgres for secret storage")
s.jimm.CredentialStore = &s.jimm.Database
}
}

s.jimm.Dialer = &jujuclient.Dialer{
ControllerCredentialsStore: vs,
ControllerCredentialsStore: s.jimm.CredentialStore,
}
if !p.DisableConnectionCache {
s.jimm.Dialer = jimm.CacheDialer(s.jimm.Dialer)
Expand Down Expand Up @@ -472,6 +462,29 @@ func newAuthenticator(ctx context.Context, db *db.Database, client *ofgaClient.O
}, nil
}

func (s *Service) setupCredentialStore(ctx context.Context, p Params) error {
const op = errors.Op("newSecretStore")
vs, err := newVaultStore(ctx, p)
if err != nil {
zapctx.Error(ctx, "Vault Store error", zap.Error(err))
return errors.E(op, err)
}
if vs != nil {
s.jimm.CredentialStore = vs
return nil
}

// Only enable Postgres storage for secrets if explicitly enabled.
if _, ok := os.LookupEnv("INSECURE_SECRET_STORAGE"); ok {
zapctx.Warn(ctx, "using plaintext postgres for secret storage")
s.jimm.CredentialStore = &s.jimm.Database
return nil
}
// Currently jimm will start without a credential store but
// functionality will be limited.
return nil
}

func newVaultStore(ctx context.Context, p Params) (jimmcreds.CredentialStore, error) {
if p.VaultSecretFile == "" {
return nil, nil
Expand Down
41 changes: 40 additions & 1 deletion service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestDefaultService(t *testing.T) {

_, ofgaClient, cfg, err := jimmtest.SetupTestOFGAClient(c.Name())
c.Assert(err, qt.IsNil)

os.Setenv("INSECURE_SECRET_STORAGE", "enable")
svc, err := jimm.NewService(context.Background(), jimm.Params{
OpenFGAParams: jimm.OpenFGAParams{
Scheme: cfg.ApiScheme,
Expand All @@ -62,6 +62,23 @@ func TestDefaultService(t *testing.T) {
c.Check(resp.StatusCode, qt.Equals, http.StatusOK)
}

func TestServiceStartsWithoutSecretStore(t *testing.T) {
c := qt.New(t)

_, ofgaClient, cfg, err := jimmtest.SetupTestOFGAClient(c.Name())
c.Assert(err, qt.IsNil)
_, err = jimm.NewService(context.Background(), jimm.Params{
OpenFGAParams: jimm.OpenFGAParams{
Scheme: cfg.ApiScheme,
Host: cfg.ApiHost,
Store: cfg.StoreId,
Token: cfg.Credentials.Config.ApiToken,
AuthModel: ofgaClient.AuthModelId,
},
})
c.Assert(err, qt.IsNil)
}

func TestAuthenticator(t *testing.T) {
c := qt.New(t)

Expand All @@ -80,6 +97,7 @@ func TestAuthenticator(t *testing.T) {
},
}
candid := startCandid(c, &p)
os.Setenv("INSECURE_SECRET_STORAGE", "enable")
svc, err := jimm.NewService(context.Background(), p)
c.Assert(err, qt.IsNil)

Expand Down Expand Up @@ -187,6 +205,27 @@ func TestVault(t *testing.T) {
})
}

func TestPostgresSecretStore(t *testing.T) {
c := qt.New(t)

_, ofgaClient, cfg, err := jimmtest.SetupTestOFGAClient(c.Name())
c.Assert(err, qt.IsNil)

p := jimm.Params{
ControllerUUID: "6acf4fd8-32d6-49ea-b4eb-dcb9d1590c11",
OpenFGAParams: jimm.OpenFGAParams{
Scheme: cfg.ApiScheme,
Host: cfg.ApiHost,
Store: cfg.StoreId,
Token: cfg.Credentials.Config.ApiToken,
AuthModel: ofgaClient.AuthModelId,
},
}
os.Setenv("INSECURE_SECRET_STORAGE", "enable")
_, err = jimm.NewService(context.Background(), p)
c.Assert(err, qt.IsNil)
}

func TestOpenFGA(t *testing.T) {
c := qt.New(t)

Expand Down

0 comments on commit be4267f

Please sign in to comment.