VAULT-22504: Support Custom Messages in api Package (#24959)

* add methods in Sys struct to manipulate UI Custom Messages

* adding go-docs

* extracting recurring URL path into a constant

* using same stretchr/testify version as the main go.mod
This commit is contained in:
Marc Boudreau
2024-01-23 13:20:58 -05:00
committed by GitHub
parent dc625253ae
commit 24e5c2c2f3
4 changed files with 483 additions and 2 deletions

View File

@@ -21,19 +21,23 @@ require (
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2
github.com/hashicorp/hcl v1.0.0
github.com/mitchellh/mapstructure v1.5.0
github.com/stretchr/testify v1.8.4
golang.org/x/net v0.17.0
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/google/go-cmp v0.5.7 // indirect
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

View File

@@ -64,8 +64,9 @@ github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIH
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190911031432-227b76d455e7/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
@@ -88,6 +89,8 @@ golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 h1:NusfzzA6yGQ+ua51ck7E3omNUX/JuqbFSaRGqU8CcLI=
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -0,0 +1,281 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package api
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
)
const (
// baseEndpoint is the common base URL path for all endpoints used in this
// module.
baseEndpoint string = "/v1/sys/config/ui/custom-messages"
)
// ListUICustomMessages calls ListUICustomMessagesWithContext using a background
// Context.
func (c *Sys) ListUICustomMessages(req UICustomMessageListRequest) (*Secret, error) {
return c.ListUICustomMessagesWithContext(context.Background(), req)
}
// ListUICustomMessagesWithContext sends a request to the List custom messages
// endpoint using the provided Context and UICustomMessageListRequest value as
// the inputs. It returns a pointer to a Secret if a response was obtained from
// the server, including error responses; or an error if a response could not be
// obtained due to an error.
func (c *Sys) ListUICustomMessagesWithContext(ctx context.Context, req UICustomMessageListRequest) (*Secret, error) {
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()
r := c.c.NewRequest("LIST", fmt.Sprintf("%s/", baseEndpoint))
if req.Active != nil {
r.Params.Add("active", strconv.FormatBool(*req.Active))
}
if req.Authenticated != nil {
r.Params.Add("authenticated", strconv.FormatBool(*req.Authenticated))
}
if req.Type != nil {
r.Params.Add("type", *req.Type)
}
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")
}
return secret, nil
}
// CreateUICustomMessage calls CreateUICustomMessageWithContext using a
// background Context.
func (c *Sys) CreateUICustomMessage(req UICustomMessageRequest) (*Secret, error) {
return c.CreateUICustomMessageWithContext(context.Background(), req)
}
// CreateUICustomMessageWithContext sends a request to the Create custom
// messages endpoint using the provided Context and UICustomMessageRequest
// values as the inputs. It returns a pointer to a Secret if a response was
// obtained from the server, including error responses; or an error if a
// response could not be obtained due to an error.
func (c *Sys) CreateUICustomMessageWithContext(ctx context.Context, req UICustomMessageRequest) (*Secret, error) {
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()
r := c.c.NewRequest(http.MethodPost, baseEndpoint)
if err := r.SetJSONBody(&req); err != nil {
return nil, fmt.Errorf("error encoding request body to json: %w", err)
}
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, fmt.Errorf("error sending request to server: %w", err)
}
defer resp.Body.Close()
secret, err := ParseSecret(resp.Body)
if err != nil {
return nil, fmt.Errorf("could not parse secret from server response: %w", err)
}
if secret == nil || secret.Data == nil {
return nil, errors.New("data from server response is empty")
}
return secret, nil
}
// ReadUICustomMessage calls ReadUICustomMessageWithContext using a background
// Context.
func (c *Sys) ReadUICustomMessage(id string) (*Secret, error) {
return c.ReadUICustomMessageWithContext(context.Background(), id)
}
// ReadUICustomMessageWithContext sends a request to the Read custom message
// endpoint using the provided Context and id values. It returns a pointer to a
// Secret if a response was obtained from the server, including error responses;
// or an error if a response could not be obtained due to an error.
func (c *Sys) ReadUICustomMessageWithContext(ctx context.Context, id string) (*Secret, error) {
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()
r := c.c.NewRequest(http.MethodGet, fmt.Sprintf("%s/%s", baseEndpoint, id))
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return nil, fmt.Errorf("error sending request to server: %w", err)
}
defer resp.Body.Close()
secret, err := ParseSecret(resp.Body)
if err != nil {
return nil, fmt.Errorf("could not parse secret from server response: %w", err)
}
if secret == nil || secret.Data == nil {
return nil, errors.New("data from server response is empty")
}
return secret, nil
}
// UpdateUICustomMessage calls UpdateUICustomMessageWithContext using a
// background Context.
func (c *Sys) UpdateUICustomMessage(id string, req UICustomMessageRequest) error {
return c.UpdateUICustomMessageWithContext(context.Background(), id, req)
}
// UpdateUICustomMessageWithContext sends a request to the Update custom message
// endpoint using the provided Context, id, and UICustomMessageRequest values.
// It returns a pointer to a Secret if a response was obtained from the server,
// including error responses; or an error if a response could not be obtained
// due to an error.
func (c *Sys) UpdateUICustomMessageWithContext(ctx context.Context, id string, req UICustomMessageRequest) error {
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()
r := c.c.NewRequest(http.MethodPost, fmt.Sprintf("%s/%s", baseEndpoint, id))
if err := r.SetJSONBody(&req); err != nil {
return fmt.Errorf("error encoding request body to json: %w", err)
}
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return fmt.Errorf("error sending request to server: %w", err)
}
defer resp.Body.Close()
return nil
}
// DeleteUICustomMessage calls DeleteUICustomMessageWithContext using a
// background Context.
func (c *Sys) DeleteUICustomMessage(id string) error {
return c.DeletePolicyWithContext(context.Background(), id)
}
// DeleteUICustomMessageWithContext sends a request to the Delete custom message
// endpoint using the provided Context and id values. It returns a pointer to a
// Secret if a response was obtained from the server, including error responses;
// or an error if a response could not be obtained due to an error.
func (c *Sys) DeleteUICustomMessageWithContext(ctx context.Context, id string) error {
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
defer cancelFunc()
r := c.c.NewRequest(http.MethodDelete, fmt.Sprintf("%s/%s", baseEndpoint, id))
resp, err := c.c.rawRequestWithContext(ctx, r)
if err != nil {
return fmt.Errorf("error sending request to server: %w", err)
}
defer resp.Body.Close()
return nil
}
// UICustomMessageListRequest is a struct used to contain inputs for the List
// custom messages request. Each field is optional, so their types are pointers.
// The With... methods can be used to easily set the fields with pointers to
// values.
type UICustomMessageListRequest struct {
Authenticated *bool
Type *string
Active *bool
}
// WithAuthenticated sets the Authenticated field to a pointer referencing the
// provided bool value.
func (r *UICustomMessageListRequest) WithAuthenticated(value bool) *UICustomMessageListRequest {
r.Authenticated = &value
return r
}
// WithType sets the Type field to a pointer referencing the provided string
// value.
func (r *UICustomMessageListRequest) WithType(value string) *UICustomMessageListRequest {
r.Type = &value
return r
}
// WithActive sets the Active field to a pointer referencing the provided bool
// value.
func (r *UICustomMessageListRequest) WithActive(value bool) *UICustomMessageListRequest {
r.Active = &value
return r
}
// UICustomMessageRequest is a struct containing the properties of a custom
// message. The Link field can be set using the WithLink method.
type UICustomMessageRequest struct {
Title string `json:"title"`
Message string `json:"message"`
Authenticated bool `json:"authenticated"`
Type string `json:"type"`
StartTime string `json:"start_time"`
EndTime string `json:"end_time,omitempty"`
Link *uiCustomMessageLink `json:"link,omitempty"`
Options map[string]any `json:"options,omitempty"`
}
// WithLink sets the Link field to the address of a new uiCustomMessageLink
// struct constructed from the provided title and href values.
func (r *UICustomMessageRequest) WithLink(title, href string) *UICustomMessageRequest {
r.Link = &uiCustomMessageLink{
Title: title,
Href: href,
}
return r
}
// uiCustomMessageLink is a utility struct used to represent a link associated
// with a custom message.
type uiCustomMessageLink struct {
Title string
Href string
}
// MarshalJSON encodes the state of the receiver uiCustomMessageLink as JSON and
// returns those encoded bytes or an error.
func (l uiCustomMessageLink) MarshalJSON() ([]byte, error) {
m := make(map[string]string)
m[l.Title] = l.Href
return json.Marshal(m)
}
// UnmarshalJSON updates the state of the receiver uiCustomMessageLink from the
// provided JSON encoded bytes. It returns an error if there was a failure.
func (l *uiCustomMessageLink) UnmarshalJSON(b []byte) error {
m := make(map[string]string)
if err := json.Unmarshal(b, &m); err != nil {
return err
}
for k, v := range m {
l.Title = k
l.Href = v
break
}
return nil
}

View File

@@ -0,0 +1,193 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package api
import (
"encoding/base64"
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
var messageBase64 string = base64.StdEncoding.EncodeToString([]byte("message"))
// TestUICustomMessageJsonMarshalling verifies that json marshalling (struct to
// json) works with the uiCustomMessageRequest type.
func TestUICustomMessageJsonMarshalling(t *testing.T) {
for _, testcase := range []struct {
name string
request UICustomMessageRequest
expectedJSON string
}{
{
name: "no-link-no-options",
request: UICustomMessageRequest{
Title: "title",
Message: messageBase64,
StartTime: "2024-01-01T00:00:00.000Z",
EndTime: "",
Type: "banner",
Authenticated: true,
},
expectedJSON: fmt.Sprintf(`{"title":"title","message":"%s","authenticated":true,"type":"banner","start_time":"2024-01-01T00:00:00.000Z"}`, messageBase64),
},
{
name: "link-no-options",
request: UICustomMessageRequest{
Title: "title",
Message: messageBase64,
StartTime: "2024-01-01T00:00:00.000Z",
EndTime: "",
Type: "modal",
Authenticated: false,
Link: &uiCustomMessageLink{
Title: "Click here",
Href: "https://www.example.org",
},
},
expectedJSON: fmt.Sprintf(`{"title":"title","message":"%s","authenticated":false,"type":"modal","start_time":"2024-01-01T00:00:00.000Z","link":{"Click here":"https://www.example.org"}}`, messageBase64),
},
{
name: "no-link-options",
request: UICustomMessageRequest{
Title: "title",
Message: messageBase64,
StartTime: "2024-01-01T00:00:00.000Z",
EndTime: "",
Authenticated: true,
Type: "banner",
Options: map[string]any{
"key": "value",
},
},
expectedJSON: fmt.Sprintf(`{"title":"title","message":"%s","authenticated":true,"type":"banner","start_time":"2024-01-01T00:00:00.000Z","options":{"key":"value"}}`, messageBase64),
},
{
name: "link-and-options",
request: UICustomMessageRequest{
Title: "title",
Message: messageBase64,
StartTime: "2024-01-01T00:00:00.000Z",
EndTime: "",
Authenticated: true,
Type: "banner",
Link: &uiCustomMessageLink{
Title: "Click here",
Href: "https://www.example.org",
},
Options: map[string]any{
"key": "value",
},
},
expectedJSON: fmt.Sprintf(`{"title":"title","message":"%s","authenticated":true,"type":"banner","start_time":"2024-01-01T00:00:00.000Z","link":{"Click here":"https://www.example.org"},"options":{"key":"value"}}`, messageBase64),
},
} {
tc := testcase
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
bytes, err := json.Marshal(&tc.request)
assert.NoError(t, err)
assert.Equal(t, tc.expectedJSON, string(bytes))
})
}
}
// TestUICustomMessageJsonUnmarshal verifies that json unmarshalling (json to
// struct) works with the uiCustomMessageRequest type.
func TestUICustomMessageJsonUnmarshal(t *testing.T) {
for _, testcase := range []struct {
name string
encodedBytes string
linkAssertion func(assert.TestingT, any, ...any) bool
checkLink bool
optionsAssertion func(assert.TestingT, any, ...any) bool
checkOptions bool
}{
{
name: "no-link-no-options",
encodedBytes: fmt.Sprintf(`{"title":"title","message":"%s","authenticated":false,"type":"modal","start_time":"2024-01-01T00:00:00.000Z"}`, messageBase64),
linkAssertion: assert.Nil,
optionsAssertion: assert.Nil,
},
{
name: "link-no-options",
encodedBytes: fmt.Sprintf(`{"title":"title","message":"%s","authenticated":false,"type":"modal","start_time":"2024-01-01T00:00:00.000Z","link":{"Click here":"https://www.example.org"}}`, messageBase64),
linkAssertion: assert.NotNil,
checkLink: true,
optionsAssertion: assert.Nil,
},
{
name: "no-link-options",
encodedBytes: fmt.Sprintf(`{"title":"title","message":"%s","authenticated":false,"type":"modal","start_time":"2024-01-01T00:00:00.000Z","options":{"key":"value"}}`, messageBase64),
linkAssertion: assert.Nil,
optionsAssertion: assert.NotNil,
checkOptions: true,
},
{
name: "link-and-options",
encodedBytes: fmt.Sprintf(`{"title":"title","message":"%s","authenticated":false,"type":"modal","start_time":"2024-01-01T00:00:00.000Z","link":{"Click here":"https://www.example.org"},"options":{"key":"value"}}`, messageBase64),
linkAssertion: assert.NotNil,
checkLink: true,
optionsAssertion: assert.NotNil,
checkOptions: true,
},
} {
tc := testcase
t.Run(testcase.name, func(t *testing.T) {
t.Parallel()
var request UICustomMessageRequest
err := json.Unmarshal([]byte(tc.encodedBytes), &request)
assert.NoError(t, err)
tc.linkAssertion(t, request.Link)
tc.optionsAssertion(t, request.Options)
if tc.checkLink {
assert.Equal(t, "Click here", request.Link.Title)
assert.Equal(t, "https://www.example.org", request.Link.Href)
}
if tc.checkOptions {
assert.Contains(t, request.Options, "key")
}
})
}
}
// TestUICustomMessageListRequestOptions verifies the correct behaviour of all
// of the With... methods of the UICustomMessageListRequest.
func TestUICustomMessageListRequestOptions(t *testing.T) {
request := &UICustomMessageListRequest{}
assert.Nil(t, request.Active)
assert.Nil(t, request.Authenticated)
assert.Nil(t, request.Type)
request = (&UICustomMessageListRequest{}).WithActive(true)
assert.NotNil(t, request.Active)
assert.True(t, *request.Active)
request = (&UICustomMessageListRequest{}).WithActive(false)
assert.NotNil(t, request.Active)
assert.False(t, *request.Active)
request = (&UICustomMessageListRequest{}).WithAuthenticated(true)
assert.NotNil(t, request.Authenticated)
assert.True(t, *request.Authenticated)
request = (&UICustomMessageListRequest{}).WithAuthenticated(false)
assert.NotNil(t, request.Authenticated)
assert.False(t, *request.Authenticated)
request = (&UICustomMessageListRequest{}).WithType("banner")
assert.NotNil(t, request.Type)
assert.Equal(t, "banner", *request.Type)
request = (&UICustomMessageListRequest{}).WithType("modal")
assert.NotNil(t, request.Type)
assert.Equal(t, "modal", *request.Type)
}