Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: add wrapper functions for GET auth and mount #25499

Merged
merged 7 commits into from
Feb 20, 2024
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
33 changes: 33 additions & 0 deletions api/sys_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,39 @@ import (
"github.com/mitchellh/mapstructure"
)

func (c *Sys) GetAuth(path string) (*AuthMount, error) {
return c.GetAuthWithContext(context.Background(), path)
}
Comment on lines +15 to +17
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this one if we have the one below? From my experience, usually we add the *WithContext and keep the one method without *WithContext for backwards compatibility, but since these are both new, I think we could get away with just having GetAuthWithContext.

With that said, the Vault conventions could be different.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had the same question but kept it for consistency.


func (c *Sys) GetAuthWithContext(ctx context.Context, path string) (*AuthMount, error) {
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.c.NewRequest(http.MethodGet, fmt.Sprintf("/v1/sys/auth/%s", path))

resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
defer resp.Body.Close()

secret, err := ParseSecret(resp.Body)
if err != nil {
return nil, err
}
if secret == nil || secret.Data == nil {
return nil, errors.New("data from server response is empty")
}

mount := AuthMount{}
err = mapstructure.Decode(secret.Data, &mount)
if err != nil {
return nil, err
}

return &mount, nil
}

func (c *Sys) ListAuth() (map[string]*AuthMount, error) {
return c.ListAuthWithContext(context.Background())
}
Expand Down
33 changes: 33 additions & 0 deletions api/sys_mounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,39 @@ import (
"github.com/mitchellh/mapstructure"
)

func (c *Sys) GetMount(path string) (*MountOutput, error) {
return c.GetMountWithContext(context.Background(), path)
}

func (c *Sys) GetMountWithContext(ctx context.Context, path string) (*MountOutput, error) {
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()

r := c.c.NewRequest(http.MethodGet, fmt.Sprintf("/v1/sys/mounts/%s", path))

resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, err
}
defer resp.Body.Close()

secret, err := ParseSecret(resp.Body)
if err != nil {
return nil, err
}
if secret == nil || secret.Data == nil {
return nil, errors.New("data from server response is empty")
}

mount := MountOutput{}
err = mapstructure.Decode(secret.Data, &mount)
if err != nil {
return nil, err
}

return &mount, nil
}

func (c *Sys) ListMounts() (map[string]*MountOutput, error) {
return c.ListMountsWithContext(context.Background())
}
Expand Down
3 changes: 3 additions & 0 deletions changelog/25499.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
api: Add wrapper functions for GET /sys/mounts/:path and GET /sys/auth/:path
```
90 changes: 90 additions & 0 deletions vault/external_tests/api/sys_auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package api

import (
"testing"

"github.com/hashicorp/vault/api"
)

// TestGetAuth tests that we can get a single auth mount
func TestGetAuth(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
mountName string
authInput *api.EnableAuthOptions
expected *api.AuthMount
shouldMount bool
expectErr bool
}{
{
name: "get-default-auth-mount-success",
mountName: "token",
authInput: nil,
expected: &api.AuthMount{
Type: "token",
},
shouldMount: false,
expectErr: false,
},
{
name: "get-manual-auth-mount-success",
mountName: "userpass",
authInput: &api.EnableAuthOptions{
Type: "userpass",
},
expected: &api.AuthMount{
Type: "userpass",
},
shouldMount: true,
expectErr: false,
},
{
name: "error-not-found",
mountName: "not-found",
authInput: nil,
expected: &api.AuthMount{
Type: "not-found",
},
shouldMount: false,
expectErr: true,
},
}

for _, tc := range testCases {
tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

client, closer := testVaultServer(t)
defer closer()

if tc.shouldMount {
err := client.Sys().EnableAuthWithOptions(tc.mountName, tc.authInput)
if err != nil {
t.Fatal(err)
}
}

mount, err := client.Sys().GetAuth(tc.mountName)
if !tc.expectErr && err != nil {
t.Fatal(err)
}

if !tc.expectErr {
if tc.expected.Type != mount.Type || tc.expected.PluginVersion != mount.PluginVersion {
t.Errorf("mount did not match: expected %+v but got %+v", tc.expected, mount)
}
} else {
if err == nil {
t.Errorf("expected error but got nil")
}
}
})
}
}
90 changes: 90 additions & 0 deletions vault/external_tests/api/sys_mounts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package api

import (
"testing"

"github.com/hashicorp/vault/api"
)

// TestGetMount tests that we can get a single secret mount
func TestGetMount(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
mountName string
mountInput *api.MountInput
expected *api.MountOutput
shouldMount bool
expectErr bool
}{
{
name: "get-default-mount-success",
mountName: "secret",
mountInput: nil,
expected: &api.MountOutput{
Type: "kv",
},
shouldMount: false,
expectErr: false,
},
{
name: "get-manual-mount-success",
mountName: "pki",
mountInput: &api.MountInput{
Type: "pki",
},
expected: &api.MountOutput{
Type: "pki",
},
shouldMount: true,
expectErr: false,
},
{
name: "error-not-found",
mountName: "not-found",
mountInput: nil,
expected: &api.MountOutput{
Type: "not-found",
},
shouldMount: false,
expectErr: true,
},
}

for _, tc := range testCases {
tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

client, closer := testVaultServer(t)
defer closer()

if tc.shouldMount {
err := client.Sys().Mount(tc.mountName, tc.mountInput)
if err != nil {
t.Fatal(err)
}
}

mount, err := client.Sys().GetMount(tc.mountName)
if !tc.expectErr && err != nil {
t.Fatal(err)
}

if !tc.expectErr {
if tc.expected.Type != mount.Type || tc.expected.PluginVersion != mount.PluginVersion {
t.Errorf("mount did not match: expected %+v but got %+v", tc.expected, mount)
}
} else {
if err == nil {
t.Errorf("expected error but got nil")
}
}
})
}
}
Loading