2023-03-15 19:00:52 +03:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-06-17 07:04:55 +03:00
|
|
|
package api
|
|
|
|
|
2018-08-14 22:29:22 +03:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2022-03-24 20:58:03 +03:00
|
|
|
"net/http"
|
2018-08-14 22:29:22 +03:00
|
|
|
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
2018-07-25 01:49:55 +03:00
|
|
|
|
2017-06-17 07:04:55 +03:00
|
|
|
func (c *Sys) CORSStatus() (*CORSResponse, error) {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.CORSStatusWithContext(context.Background())
|
|
|
|
}
|
2018-07-25 01:49:55 +03:00
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
func (c *Sys) CORSStatusWithContext(ctx context.Context) (*CORSResponse, 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/config/cors")
|
2022-03-24 00:47:43 +03:00
|
|
|
|
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2017-06-17 07:04:55 +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)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if secret == nil || secret.Data == nil {
|
|
|
|
return nil, errors.New("data from server response is empty")
|
|
|
|
}
|
|
|
|
|
2017-06-17 07:04:55 +03:00
|
|
|
var result CORSResponse
|
2018-08-14 22:29:22 +03:00
|
|
|
err = mapstructure.Decode(secret.Data, &result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-06-17 07:04:55 +03:00
|
|
|
return &result, err
|
|
|
|
}
|
|
|
|
|
2020-12-03 16:31:25 +03:00
|
|
|
func (c *Sys) ConfigureCORS(req *CORSRequest) error {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.ConfigureCORSWithContext(context.Background(), req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Sys) ConfigureCORSWithContext(ctx context.Context, req *CORSRequest) error {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
2022-03-24 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodPut, "/v1/sys/config/cors")
|
2017-06-17 07:04:55 +03:00
|
|
|
if err := r.SetJSONBody(req); err != nil {
|
2020-12-03 16:31:25 +03:00
|
|
|
return err
|
2017-06-17 07:04:55 +03:00
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2020-12-03 16:31:25 +03:00
|
|
|
if err == nil {
|
|
|
|
defer resp.Body.Close()
|
2018-08-14 22:29:22 +03:00
|
|
|
}
|
2020-12-03 16:31:25 +03:00
|
|
|
return err
|
2017-06-17 07:04:55 +03:00
|
|
|
}
|
|
|
|
|
2020-12-03 16:31:25 +03:00
|
|
|
func (c *Sys) DisableCORS() error {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.DisableCORSWithContext(context.Background())
|
|
|
|
}
|
2017-06-17 07:04:55 +03:00
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
func (c *Sys) DisableCORSWithContext(ctx context.Context) 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, "/v1/sys/config/cors")
|
2022-03-24 00:47:43 +03:00
|
|
|
|
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2020-12-03 16:31:25 +03:00
|
|
|
if err == nil {
|
|
|
|
defer resp.Body.Close()
|
2018-08-14 22:29:22 +03:00
|
|
|
}
|
2020-12-03 16:31:25 +03:00
|
|
|
return err
|
2017-06-17 07:04:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type CORSRequest struct {
|
2020-12-03 16:31:25 +03:00
|
|
|
AllowedOrigins []string `json:"allowed_origins" mapstructure:"allowed_origins"`
|
|
|
|
AllowedHeaders []string `json:"allowed_headers" mapstructure:"allowed_headers"`
|
|
|
|
Enabled bool `json:"enabled" mapstructure:"enabled"`
|
2017-06-17 07:04:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type CORSResponse struct {
|
2020-12-03 16:31:25 +03:00
|
|
|
AllowedOrigins []string `json:"allowed_origins" mapstructure:"allowed_origins"`
|
|
|
|
AllowedHeaders []string `json:"allowed_headers" mapstructure:"allowed_headers"`
|
|
|
|
Enabled bool `json:"enabled" mapstructure:"enabled"`
|
2017-06-17 07:04:55 +03:00
|
|
|
}
|