2023-03-15 19:00:52 +03:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2016-01-09 05:21:02 +03:00
|
|
|
package api
|
|
|
|
|
2022-03-24 20:58:03 +03:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
)
|
2018-07-25 01:49:55 +03:00
|
|
|
|
2016-01-15 18:55:35 +03:00
|
|
|
func (c *Sys) GenerateRootStatus() (*GenerateRootStatusResponse, error) {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.GenerateRootStatusWithContext(context.Background())
|
2017-11-10 21:19:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) GenerateDROperationTokenStatus() (*GenerateRootStatusResponse, error) {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.GenerateDROperationTokenStatusWithContext(context.Background())
|
2017-11-10 21:19:42 +03:00
|
|
|
}
|
|
|
|
|
2019-10-15 07:55:31 +03:00
|
|
|
func (c *Sys) GenerateRecoveryOperationTokenStatus() (*GenerateRootStatusResponse, error) {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.GenerateRecoveryOperationTokenStatusWithContext(context.Background())
|
2019-10-15 07:55:31 +03:00
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
func (c *Sys) GenerateRootStatusWithContext(ctx context.Context) (*GenerateRootStatusResponse, error) {
|
|
|
|
return c.generateRootStatusCommonWithContext(ctx, "/v1/sys/generate-root/attempt")
|
|
|
|
}
|
2018-07-25 01:49:55 +03:00
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
func (c *Sys) GenerateDROperationTokenStatusWithContext(ctx context.Context) (*GenerateRootStatusResponse, error) {
|
|
|
|
return c.generateRootStatusCommonWithContext(ctx, "/v1/sys/replication/dr/secondary/generate-operation-token/attempt")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) GenerateRecoveryOperationTokenStatusWithContext(ctx context.Context) (*GenerateRootStatusResponse, error) {
|
|
|
|
return c.generateRootStatusCommonWithContext(ctx, "/v1/sys/generate-recovery-token/attempt")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) generateRootStatusCommonWithContext(ctx context.Context, path string) (*GenerateRootStatusResponse, 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, path)
|
2022-03-24 00:47:43 +03:00
|
|
|
|
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2016-01-09 05:21:02 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2016-01-15 18:55:35 +03:00
|
|
|
var result GenerateRootStatusResponse
|
2016-01-09 05:21:02 +03:00
|
|
|
err = resp.DecodeJSON(&result)
|
|
|
|
return &result, err
|
|
|
|
}
|
|
|
|
|
2016-02-12 22:24:36 +03:00
|
|
|
func (c *Sys) GenerateRootInit(otp, pgpKey string) (*GenerateRootStatusResponse, error) {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.GenerateRootInitWithContext(context.Background(), otp, pgpKey)
|
2017-11-10 21:19:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) GenerateDROperationTokenInit(otp, pgpKey string) (*GenerateRootStatusResponse, error) {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.GenerateDROperationTokenInitWithContext(context.Background(), otp, pgpKey)
|
2017-11-10 21:19:42 +03:00
|
|
|
}
|
|
|
|
|
2019-10-15 07:55:31 +03:00
|
|
|
func (c *Sys) GenerateRecoveryOperationTokenInit(otp, pgpKey string) (*GenerateRootStatusResponse, error) {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.GenerateRecoveryOperationTokenInitWithContext(context.Background(), otp, pgpKey)
|
2019-10-15 07:55:31 +03:00
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
func (c *Sys) GenerateRootInitWithContext(ctx context.Context, otp, pgpKey string) (*GenerateRootStatusResponse, error) {
|
|
|
|
return c.generateRootInitCommonWithContext(ctx, "/v1/sys/generate-root/attempt", otp, pgpKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) GenerateDROperationTokenInitWithContext(ctx context.Context, otp, pgpKey string) (*GenerateRootStatusResponse, error) {
|
|
|
|
return c.generateRootInitCommonWithContext(ctx, "/v1/sys/replication/dr/secondary/generate-operation-token/attempt", otp, pgpKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) GenerateRecoveryOperationTokenInitWithContext(ctx context.Context, otp, pgpKey string) (*GenerateRootStatusResponse, error) {
|
|
|
|
return c.generateRootInitCommonWithContext(ctx, "/v1/sys/generate-recovery-token/attempt", otp, pgpKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) generateRootInitCommonWithContext(ctx context.Context, path, otp, pgpKey string) (*GenerateRootStatusResponse, error) {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
2016-01-09 05:21:02 +03:00
|
|
|
body := map[string]interface{}{
|
|
|
|
"otp": otp,
|
|
|
|
"pgp_key": pgpKey,
|
|
|
|
}
|
|
|
|
|
2022-03-24 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodPut, path)
|
2016-01-09 05:21:02 +03:00
|
|
|
if err := r.SetJSONBody(body); err != nil {
|
2016-02-12 22:24:36 +03:00
|
|
|
return nil, err
|
2016-01-09 05:21:02 +03:00
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2016-02-12 22:24:36 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-01-09 05:21:02 +03:00
|
|
|
}
|
2016-02-12 22:24:36 +03:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var result GenerateRootStatusResponse
|
|
|
|
err = resp.DecodeJSON(&result)
|
|
|
|
return &result, err
|
2016-01-09 05:21:02 +03:00
|
|
|
}
|
|
|
|
|
2016-01-15 18:55:35 +03:00
|
|
|
func (c *Sys) GenerateRootCancel() error {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.GenerateRootCancelWithContext(context.Background())
|
2017-11-10 21:19:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) GenerateDROperationTokenCancel() error {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.GenerateDROperationTokenCancelWithContext(context.Background())
|
2017-11-10 21:19:42 +03:00
|
|
|
}
|
|
|
|
|
2019-10-15 07:55:31 +03:00
|
|
|
func (c *Sys) GenerateRecoveryOperationTokenCancel() error {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.GenerateRecoveryOperationTokenCancelWithContext(context.Background())
|
2019-10-15 07:55:31 +03:00
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
func (c *Sys) GenerateRootCancelWithContext(ctx context.Context) error {
|
|
|
|
return c.generateRootCancelCommonWithContext(ctx, "/v1/sys/generate-root/attempt")
|
|
|
|
}
|
2018-07-25 01:49:55 +03:00
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
func (c *Sys) GenerateDROperationTokenCancelWithContext(ctx context.Context) error {
|
|
|
|
return c.generateRootCancelCommonWithContext(ctx, "/v1/sys/replication/dr/secondary/generate-operation-token/attempt")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) GenerateRecoveryOperationTokenCancelWithContext(ctx context.Context) error {
|
|
|
|
return c.generateRootCancelCommonWithContext(ctx, "/v1/sys/generate-recovery-token/attempt")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) generateRootCancelCommonWithContext(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, path)
|
2022-03-24 00:47:43 +03:00
|
|
|
|
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2016-01-09 05:21:02 +03:00
|
|
|
if err == nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-15 18:55:35 +03:00
|
|
|
func (c *Sys) GenerateRootUpdate(shard, nonce string) (*GenerateRootStatusResponse, error) {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.GenerateRootUpdateWithContext(context.Background(), shard, nonce)
|
2017-11-10 21:19:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) GenerateDROperationTokenUpdate(shard, nonce string) (*GenerateRootStatusResponse, error) {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.GenerateDROperationTokenUpdateWithContext(context.Background(), shard, nonce)
|
2017-11-10 21:19:42 +03:00
|
|
|
}
|
|
|
|
|
2019-10-15 07:55:31 +03:00
|
|
|
func (c *Sys) GenerateRecoveryOperationTokenUpdate(shard, nonce string) (*GenerateRootStatusResponse, error) {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.GenerateRecoveryOperationTokenUpdateWithContext(context.Background(), shard, nonce)
|
2019-10-15 07:55:31 +03:00
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
func (c *Sys) GenerateRootUpdateWithContext(ctx context.Context, shard, nonce string) (*GenerateRootStatusResponse, error) {
|
|
|
|
return c.generateRootUpdateCommonWithContext(ctx, "/v1/sys/generate-root/update", shard, nonce)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) GenerateDROperationTokenUpdateWithContext(ctx context.Context, shard, nonce string) (*GenerateRootStatusResponse, error) {
|
|
|
|
return c.generateRootUpdateCommonWithContext(ctx, "/v1/sys/replication/dr/secondary/generate-operation-token/update", shard, nonce)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) GenerateRecoveryOperationTokenUpdateWithContext(ctx context.Context, shard, nonce string) (*GenerateRootStatusResponse, error) {
|
|
|
|
return c.generateRootUpdateCommonWithContext(ctx, "/v1/sys/generate-recovery-token/update", shard, nonce)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) generateRootUpdateCommonWithContext(ctx context.Context, path, shard, nonce string) (*GenerateRootStatusResponse, error) {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
2016-01-09 05:21:02 +03:00
|
|
|
body := map[string]interface{}{
|
|
|
|
"key": shard,
|
|
|
|
"nonce": nonce,
|
|
|
|
}
|
|
|
|
|
2022-03-24 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodPut, path)
|
2016-01-09 05:21:02 +03:00
|
|
|
if err := r.SetJSONBody(body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2016-01-09 05:21:02 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2016-01-15 18:55:35 +03:00
|
|
|
var result GenerateRootStatusResponse
|
2016-01-09 05:21:02 +03:00
|
|
|
err = resp.DecodeJSON(&result)
|
|
|
|
return &result, err
|
|
|
|
}
|
|
|
|
|
2016-01-15 18:55:35 +03:00
|
|
|
type GenerateRootStatusResponse struct {
|
2018-02-13 02:12:16 +03:00
|
|
|
Nonce string `json:"nonce"`
|
|
|
|
Started bool `json:"started"`
|
|
|
|
Progress int `json:"progress"`
|
|
|
|
Required int `json:"required"`
|
|
|
|
Complete bool `json:"complete"`
|
2017-11-13 23:44:26 +03:00
|
|
|
EncodedToken string `json:"encoded_token"`
|
2016-01-09 05:21:02 +03:00
|
|
|
EncodedRootToken string `json:"encoded_root_token"`
|
|
|
|
PGPFingerprint string `json:"pgp_fingerprint"`
|
2018-08-22 21:37:40 +03:00
|
|
|
OTP string `json:"otp"`
|
|
|
|
OTPLength int `json:"otp_length"`
|
2016-01-09 05:21:02 +03:00
|
|
|
}
|