2023-03-15 19:00:52 +03:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-03-12 03:27:39 +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-12 03:27:39 +03:00
|
|
|
"fmt"
|
2022-03-24 20:58:03 +03:00
|
|
|
"net/http"
|
2022-02-17 23:17:59 +03:00
|
|
|
"time"
|
2015-08-31 21:27:49 +03:00
|
|
|
|
2016-08-08 23:00:31 +03:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2015-03-12 03:27:39 +03:00
|
|
|
)
|
|
|
|
|
2015-09-25 16:46:20 +03:00
|
|
|
func (c *Sys) ListMounts() (map[string]*MountOutput, error) {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.ListMountsWithContext(context.Background())
|
|
|
|
}
|
2018-07-25 01:49:55 +03:00
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
func (c *Sys) ListMountsWithContext(ctx context.Context) (map[string]*MountOutput, 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/mounts")
|
2022-03-24 00:47:43 +03:00
|
|
|
|
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-03-12 03:27:39 +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]*MountOutput{}
|
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-12 03:27:39 +03:00
|
|
|
}
|
|
|
|
|
2015-09-25 16:46:20 +03:00
|
|
|
func (c *Sys) Mount(path string, mountInfo *MountInput) error {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.MountWithContext(context.Background(), path, mountInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) MountWithContext(ctx context.Context, path string, mountInfo *MountInput) 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/mounts/%s", path))
|
2018-04-10 05:23:56 +03:00
|
|
|
if err := r.SetJSONBody(mountInfo); err != nil {
|
2015-03-12 03:27:39 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-03-12 03:27:39 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) Unmount(path string) error {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.UnmountWithContext(context.Background(), path)
|
|
|
|
}
|
2018-07-25 01:49:55 +03:00
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
func (c *Sys) UnmountWithContext(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/mounts/%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-12 03:27:39 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
// Remount wraps RemountWithContext using context.Background.
|
2015-09-02 22:56:58 +03:00
|
|
|
func (c *Sys) Remount(from, to string) error {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.RemountWithContext(context.Background(), from, to)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemountWithContext kicks off a remount operation, polls the status endpoint using
|
|
|
|
// the migration ID till either success or failure state is observed
|
|
|
|
func (c *Sys) RemountWithContext(ctx context.Context, from, to string) error {
|
|
|
|
remountResp, err := c.StartRemountWithContext(ctx, from, to)
|
2022-02-17 23:17:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2022-03-24 00:47:43 +03:00
|
|
|
remountStatusResp, err := c.RemountStatusWithContext(ctx, remountResp.MigrationID)
|
2022-02-17 23:17:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if remountStatusResp.MigrationInfo.MigrationStatus == "success" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if remountStatusResp.MigrationInfo.MigrationStatus == "failure" {
|
|
|
|
return fmt.Errorf("Failure! Error encountered moving mount %s to %s, with migration ID %s", from, to, remountResp.MigrationID)
|
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
// StartRemount wraps StartRemountWithContext using context.Background.
|
2022-02-17 23:17:59 +03:00
|
|
|
func (c *Sys) StartRemount(from, to string) (*MountMigrationOutput, error) {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.StartRemountWithContext(context.Background(), from, to)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartRemountWithContext kicks off a mount migration and returns a response with the migration ID
|
|
|
|
func (c *Sys) StartRemountWithContext(ctx context.Context, from, to string) (*MountMigrationOutput, error) {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
2015-08-31 21:27:49 +03:00
|
|
|
body := map[string]interface{}{
|
2015-09-02 22:56:58 +03:00
|
|
|
"from": from,
|
|
|
|
"to": to,
|
2015-08-31 21:27:49 +03:00
|
|
|
}
|
2015-03-12 03:27:39 +03:00
|
|
|
|
2022-03-24 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodPost, "/v1/sys/remount")
|
2015-03-12 03:27:39 +03:00
|
|
|
if err := r.SetJSONBody(body); err != nil {
|
2022-02-17 23:17:59 +03:00
|
|
|
return nil, err
|
2015-03-12 03:27:39 +03:00
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2022-02-17 23:17:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-02-16 22:21:42 +03:00
|
|
|
}
|
2022-02-17 23:17:59 +03:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
var result MountMigrationOutput
|
|
|
|
err = mapstructure.Decode(secret.Data, &result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result, err
|
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
// RemountStatus wraps RemountStatusWithContext using context.Background.
|
2022-02-17 23:17:59 +03:00
|
|
|
func (c *Sys) RemountStatus(migrationID string) (*MountMigrationStatusOutput, error) {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.RemountStatusWithContext(context.Background(), migrationID)
|
|
|
|
}
|
2022-02-17 23:17:59 +03:00
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
// RemountStatusWithContext checks the status of a mount migration operation with the provided ID
|
|
|
|
func (c *Sys) RemountStatusWithContext(ctx context.Context, migrationID string) (*MountMigrationStatusOutput, error) {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
2022-02-17 23:17:59 +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, fmt.Sprintf("/v1/sys/remount/status/%s", migrationID))
|
2022-03-24 00:47:43 +03:00
|
|
|
|
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2022-02-17 23:17:59 +03:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
var result MountMigrationStatusOutput
|
|
|
|
err = mapstructure.Decode(secret.Data, &result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result, err
|
2015-03-12 03:27:39 +03:00
|
|
|
}
|
|
|
|
|
2015-09-25 16:46:20 +03:00
|
|
|
func (c *Sys) TuneMount(path string, config MountConfigInput) error {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.TuneMountWithContext(context.Background(), path, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) TuneMountWithContext(ctx context.Context, path string, config MountConfigInput) 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/mounts/%s/tune", path))
|
2018-04-10 05:17:37 +03:00
|
|
|
if err := r.SetJSONBody(config); err != nil {
|
2015-09-02 22:56:58 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-09-02 22:56:58 +03:00
|
|
|
if err == nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-03 02:11:05 +03:00
|
|
|
func (c *Sys) MountConfig(path string) (*MountConfigOutput, error) {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.MountConfigWithContext(context.Background(), path)
|
|
|
|
}
|
2015-09-02 22:56:58 +03:00
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
func (c *Sys) MountConfigWithContext(ctx context.Context, path string) (*MountConfigOutput, 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, fmt.Sprintf("/v1/sys/mounts/%s/tune", path))
|
2022-03-24 00:47:43 +03:00
|
|
|
|
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2016-02-03 02:11:05 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-09-02 22:56:58 +03:00
|
|
|
}
|
2016-02-03 02:11:05 +03:00
|
|
|
defer resp.Body.Close()
|
2016-08-08 23:00:31 +03:00
|
|
|
|
2018-08-14 22:29:22 +03:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2016-02-03 02:11:05 +03:00
|
|
|
var result MountConfigOutput
|
2018-08-14 22:29:22 +03:00
|
|
|
err = mapstructure.Decode(secret.Data, &result)
|
2016-08-08 23:00:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-02-03 02:11:05 +03:00
|
|
|
return &result, err
|
2015-09-02 22:56:58 +03:00
|
|
|
}
|
|
|
|
|
2015-09-25 16:46:20 +03:00
|
|
|
type MountInput struct {
|
2019-10-17 20:33:00 +03:00
|
|
|
Type string `json:"type"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Config MountConfigInput `json:"config"`
|
|
|
|
Local bool `json:"local"`
|
|
|
|
SealWrap bool `json:"seal_wrap" mapstructure:"seal_wrap"`
|
|
|
|
ExternalEntropyAccess bool `json:"external_entropy_access" mapstructure:"external_entropy_access"`
|
|
|
|
Options map[string]string `json:"options"`
|
2018-11-20 03:41:17 +03:00
|
|
|
|
|
|
|
// Deprecated: Newer server responses should be returning this information in the
|
|
|
|
// Type field (json: "type") instead.
|
|
|
|
PluginName string `json:"plugin_name,omitempty"`
|
2015-09-09 22:24:45 +03:00
|
|
|
}
|
|
|
|
|
2015-09-25 16:46:20 +03:00
|
|
|
type MountConfigInput struct {
|
2022-11-01 21:02:07 +03:00
|
|
|
Options map[string]string `json:"options" mapstructure:"options"`
|
|
|
|
DefaultLeaseTTL string `json:"default_lease_ttl" mapstructure:"default_lease_ttl"`
|
|
|
|
Description *string `json:"description,omitempty" mapstructure:"description"`
|
|
|
|
MaxLeaseTTL string `json:"max_lease_ttl" mapstructure:"max_lease_ttl"`
|
|
|
|
ForceNoCache bool `json:"force_no_cache" mapstructure:"force_no_cache"`
|
|
|
|
AuditNonHMACRequestKeys []string `json:"audit_non_hmac_request_keys,omitempty" mapstructure:"audit_non_hmac_request_keys"`
|
|
|
|
AuditNonHMACResponseKeys []string `json:"audit_non_hmac_response_keys,omitempty" mapstructure:"audit_non_hmac_response_keys"`
|
|
|
|
ListingVisibility string `json:"listing_visibility,omitempty" mapstructure:"listing_visibility"`
|
|
|
|
PassthroughRequestHeaders []string `json:"passthrough_request_headers,omitempty" mapstructure:"passthrough_request_headers"`
|
|
|
|
AllowedResponseHeaders []string `json:"allowed_response_headers,omitempty" mapstructure:"allowed_response_headers"`
|
|
|
|
TokenType string `json:"token_type,omitempty" mapstructure:"token_type"`
|
|
|
|
AllowedManagedKeys []string `json:"allowed_managed_keys,omitempty" mapstructure:"allowed_managed_keys"`
|
|
|
|
PluginVersion string `json:"plugin_version,omitempty"`
|
|
|
|
UserLockoutConfig *UserLockoutConfigInput `json:"user_lockout_config,omitempty"`
|
2018-11-20 03:41:17 +03:00
|
|
|
// Deprecated: This field will always be blank for newer server responses.
|
|
|
|
PluginName string `json:"plugin_name,omitempty" mapstructure:"plugin_name"`
|
2015-09-25 16:46:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type MountOutput struct {
|
2019-10-17 20:33:00 +03:00
|
|
|
UUID string `json:"uuid"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Accessor string `json:"accessor"`
|
|
|
|
Config MountConfigOutput `json:"config"`
|
|
|
|
Options map[string]string `json:"options"`
|
|
|
|
Local bool `json:"local"`
|
|
|
|
SealWrap bool `json:"seal_wrap" mapstructure:"seal_wrap"`
|
|
|
|
ExternalEntropyAccess bool `json:"external_entropy_access" mapstructure:"external_entropy_access"`
|
2022-09-20 14:35:50 +03:00
|
|
|
PluginVersion string `json:"plugin_version" mapstructure:"plugin_version"`
|
|
|
|
RunningVersion string `json:"running_plugin_version" mapstructure:"running_plugin_version"`
|
|
|
|
RunningSha256 string `json:"running_sha256" mapstructure:"running_sha256"`
|
2022-08-31 23:11:14 +03:00
|
|
|
DeprecationStatus string `json:"deprecation_status" mapstructure:"deprecation_status"`
|
2015-09-25 16:46:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type MountConfigOutput struct {
|
2022-11-01 21:02:07 +03:00
|
|
|
DefaultLeaseTTL int `json:"default_lease_ttl" mapstructure:"default_lease_ttl"`
|
|
|
|
MaxLeaseTTL int `json:"max_lease_ttl" mapstructure:"max_lease_ttl"`
|
|
|
|
ForceNoCache bool `json:"force_no_cache" mapstructure:"force_no_cache"`
|
|
|
|
AuditNonHMACRequestKeys []string `json:"audit_non_hmac_request_keys,omitempty" mapstructure:"audit_non_hmac_request_keys"`
|
|
|
|
AuditNonHMACResponseKeys []string `json:"audit_non_hmac_response_keys,omitempty" mapstructure:"audit_non_hmac_response_keys"`
|
|
|
|
ListingVisibility string `json:"listing_visibility,omitempty" mapstructure:"listing_visibility"`
|
|
|
|
PassthroughRequestHeaders []string `json:"passthrough_request_headers,omitempty" mapstructure:"passthrough_request_headers"`
|
|
|
|
AllowedResponseHeaders []string `json:"allowed_response_headers,omitempty" mapstructure:"allowed_response_headers"`
|
|
|
|
TokenType string `json:"token_type,omitempty" mapstructure:"token_type"`
|
|
|
|
AllowedManagedKeys []string `json:"allowed_managed_keys,omitempty" mapstructure:"allowed_managed_keys"`
|
|
|
|
UserLockoutConfig *UserLockoutConfigOutput `json:"user_lockout_config,omitempty"`
|
2018-11-20 03:41:17 +03:00
|
|
|
// Deprecated: This field will always be blank for newer server responses.
|
|
|
|
PluginName string `json:"plugin_name,omitempty" mapstructure:"plugin_name"`
|
2015-03-12 03:27:39 +03:00
|
|
|
}
|
2022-02-17 23:17:59 +03:00
|
|
|
|
2022-11-01 21:02:07 +03:00
|
|
|
type UserLockoutConfigInput struct {
|
|
|
|
LockoutThreshold string `json:"lockout_threshold,omitempty" structs:"lockout_threshold" mapstructure:"lockout_threshold"`
|
|
|
|
LockoutDuration string `json:"lockout_duration,omitempty" structs:"lockout_duration" mapstructure:"lockout_duration"`
|
|
|
|
LockoutCounterResetDuration string `json:"lockout_counter_reset_duration,omitempty" structs:"lockout_counter_reset_duration" mapstructure:"lockout_counter_reset_duration"`
|
|
|
|
DisableLockout *bool `json:"lockout_disable,omitempty" structs:"lockout_disable" mapstructure:"lockout_disable"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type UserLockoutConfigOutput struct {
|
|
|
|
LockoutThreshold uint `json:"lockout_threshold,omitempty" structs:"lockout_threshold" mapstructure:"lockout_threshold"`
|
|
|
|
LockoutDuration int `json:"lockout_duration,omitempty" structs:"lockout_duration" mapstructure:"lockout_duration"`
|
|
|
|
LockoutCounterReset int `json:"lockout_counter_reset,omitempty" structs:"lockout_counter_reset" mapstructure:"lockout_counter_reset"`
|
|
|
|
DisableLockout *bool `json:"disable_lockout,omitempty" structs:"disable_lockout" mapstructure:"disable_lockout"`
|
|
|
|
}
|
|
|
|
|
2022-02-17 23:17:59 +03:00
|
|
|
type MountMigrationOutput struct {
|
|
|
|
MigrationID string `mapstructure:"migration_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type MountMigrationStatusOutput struct {
|
|
|
|
MigrationID string `mapstructure:"migration_id"`
|
|
|
|
MigrationInfo *MountMigrationStatusInfo `mapstructure:"migration_info"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type MountMigrationStatusInfo struct {
|
|
|
|
SourceMount string `mapstructure:"source_mount"`
|
|
|
|
TargetMount string `mapstructure:"target_mount"`
|
|
|
|
MigrationStatus string `mapstructure:"status"`
|
|
|
|
}
|