Skip to content

Commit

Permalink
feat: ability to delete config (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
tenstad authored Sep 26, 2023
1 parent 481a728 commit 5a73eb6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
15 changes: 15 additions & 0 deletions internal/jwtauth/path_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func pathConfig(backend *multiroleJWTAuthBackend) *framework.Path {
Callback: backend.pathConfigWrite,
Summary: "Configure the multirole JWT authentication backend.",
},
logical.DeleteOperation: &framework.PathOperation{
Callback: backend.pathConfigDelete,
Summary: "Delete the multirole JWT authentication backend config.",
},
},

HelpSynopsis: pathConfigHelpSyn,
Expand Down Expand Up @@ -138,3 +142,14 @@ func (b *multiroleJWTAuthBackend) pathConfigRead(
},
}, nil
}

func (b *multiroleJWTAuthBackend) pathConfigDelete(
ctx context.Context, req *logical.Request, d *framework.FieldData,
) (*logical.Response, error) {
err := req.Storage.Delete(ctx, configPath)
if err == nil {
b.reset()
}

return nil, err
}
46 changes: 46 additions & 0 deletions internal/jwtauth/path_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,52 @@ func TestConfig_Read(t *testing.T) {
}
}

func TestConfig_Delete(t *testing.T) {
t.Parallel()
backend, storage := createTestBackend(t)

configData := testConfig()
req := &logical.Request{
Operation: logical.UpdateOperation,
Path: configPath,
Storage: storage,
Data: configData,
}

resp, err := backend.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err:%s resp:%#v\n", err, resp)
}

conf, err := backend.(*multiroleJWTAuthBackend).config(context.Background(), storage)
if err != nil {
t.Fatal(err)
}
if conf == nil {
t.Fatal("expected config to exist after write")
}

req = &logical.Request{
Operation: logical.DeleteOperation,
Path: configPath,
Storage: storage,
Data: nil,
}

resp, err = backend.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err:%s resp:%#v\n", err, resp)
}

conf, err = backend.(*multiroleJWTAuthBackend).config(context.Background(), storage)
if err != nil {
t.Fatal(err)
}
if conf != nil {
t.Fatal("expected config to not exist after delete")
}
}

func createTestBackend(t *testing.T) (logical.Backend, logical.Storage) {
config := &logical.BackendConfig{
Logger: logging.NewVaultLogger(log.Trace),
Expand Down

0 comments on commit 5a73eb6

Please sign in to comment.