2023-03-15 19:00:52 +03:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-03-11 22:03:28 +03:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2018-07-25 01:49:55 +03:00
|
|
|
"context"
|
2018-08-14 22:29:22 +03:00
|
|
|
"errors"
|
2015-03-11 22:03:28 +03:00
|
|
|
"fmt"
|
2022-03-24 20:58:03 +03:00
|
|
|
"net/http"
|
2016-08-08 23:00:31 +03:00
|
|
|
|
|
|
|
"github.com/mitchellh/mapstructure"
|
2015-03-11 22:03:28 +03:00
|
|
|
)
|
|
|
|
|
2015-04-05 03:53:59 +03:00
|
|
|
func (c *Sys) ListAuth() (map[string]*AuthMount, error) {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.ListAuthWithContext(context.Background())
|
|
|
|
}
|
2018-07-25 01:49:55 +03:00
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
func (c *Sys) ListAuthWithContext(ctx context.Context) (map[string]*AuthMount, error) {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
2018-07-25 01:49:55 +03:00
|
|
|
defer cancelFunc()
|
2022-03-24 00:47:43 +03:00
|
|
|
|
2022-03-24 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodGet, "/v1/sys/auth")
|
2022-03-24 00:47:43 +03:00
|
|
|
|
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-03-11 22:03:28 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2018-08-14 22:29:22 +03:00
|
|
|
secret, err := ParseSecret(resp.Body)
|
2016-08-08 23:00:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-14 22:29:22 +03:00
|
|
|
if secret == nil || secret.Data == nil {
|
|
|
|
return nil, errors.New("data from server response is empty")
|
|
|
|
}
|
2016-08-08 23:00:31 +03:00
|
|
|
|
2016-08-14 21:52:45 +03:00
|
|
|
mounts := map[string]*AuthMount{}
|
2018-08-14 22:29:22 +03:00
|
|
|
err = mapstructure.Decode(secret.Data, &mounts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-08-08 23:00:31 +03:00
|
|
|
}
|
|
|
|
|
2016-08-14 21:52:45 +03:00
|
|
|
return mounts, nil
|
2015-03-11 22:03:28 +03:00
|
|
|
}
|
|
|
|
|
2017-02-16 19:37:27 +03:00
|
|
|
// DEPRECATED: Use EnableAuthWithOptions instead
|
2015-04-01 06:27:55 +03:00
|
|
|
func (c *Sys) EnableAuth(path, authType, desc string) error {
|
2017-02-16 19:37:27 +03:00
|
|
|
return c.EnableAuthWithOptions(path, &EnableAuthOptions{
|
|
|
|
Type: authType,
|
|
|
|
Description: desc,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) EnableAuthWithOptions(path string, options *EnableAuthOptions) error {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.EnableAuthWithOptionsWithContext(context.Background(), path, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) EnableAuthWithOptionsWithContext(ctx context.Context, path string, options *EnableAuthOptions) error {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
2022-03-24 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodPost, fmt.Sprintf("/v1/sys/auth/%s", path))
|
2018-04-10 05:23:56 +03:00
|
|
|
if err := r.SetJSONBody(options); err != nil {
|
2015-03-11 22:03:28 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-03-11 22:03:28 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-01 06:27:55 +03:00
|
|
|
func (c *Sys) DisableAuth(path string) error {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.DisableAuthWithContext(context.Background(), path)
|
|
|
|
}
|
2018-07-25 01:49:55 +03:00
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
func (c *Sys) DisableAuthWithContext(ctx context.Context, path string) error {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
2018-07-25 01:49:55 +03:00
|
|
|
defer cancelFunc()
|
2022-03-24 00:47:43 +03:00
|
|
|
|
2022-03-24 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodDelete, fmt.Sprintf("/v1/sys/auth/%s", path))
|
2022-03-24 00:47:43 +03:00
|
|
|
|
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-04-01 06:27:55 +03:00
|
|
|
if err == nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
2015-03-11 22:03:28 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-15 19:56:24 +03:00
|
|
|
// Rather than duplicate, we can use modern Go's type aliasing
|
2021-04-08 19:43:39 +03:00
|
|
|
type (
|
|
|
|
EnableAuthOptions = MountInput
|
|
|
|
AuthConfigInput = MountConfigInput
|
|
|
|
AuthMount = MountOutput
|
|
|
|
AuthConfigOutput = MountConfigOutput
|
|
|
|
)
|