-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Uml 2978 Add System Message feature to Admin portal (#2452)
* fix keys * created system message page to admin portal * refactoring system message page * gohtml template layout * started connecting up to parameter store code (WIP) * test for server.go is passing now * WIP - getting tests to work * test now working * write method and test WIP not yet working * test for system message, uses prefix in ssm connection * mocked out service in tests (WIP) * WIP textareas are populated by hard-coded strings - next step they will come from the paramter store via the service * call service to get textarea values * fix golint errors * golint fix * WIP now saves to localhost parameter store but handler needs fixing * WIP fixes * refactored template and amended handler * added overwrite to ssm PutSystemMessage * added test for PutParameter functionality * refactor of handler with bilingual message validation check * Added delete message & success/error messages * service message removed message * tests for data/system_message * tests for systemmessage handler * reformatting for linting etc * linting fix * later onelogin mock * use SSM URL * test coverage for system message handler parse form error * refactoring system message handler test. * log endpoint on startup, to debug issue in aws * log to stdout * change env var * Try with no baseendpoint * fix linter error * fix linter errors * fix linter * Fix SSM endpoint * amended to systemmessage handler test to include no validation error * added pointer * lint * extra test coverage - data side * env var * Add environment name * use environment name as prefix when accessing paramter store * fixes * use /system-message/ENV/en/cy for for paramter store * fix * fix prefix logic * keep linter happy / improve comment --------- Co-authored-by: Lowenna Baggaley <[email protected]> Co-authored-by: Sam Ainsworth <[email protected]>
- Loading branch information
1 parent
02f608a
commit 7763413
Showing
19 changed files
with
950 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
74 changes: 0 additions & 74 deletions
74
service-admin/internal/server/data/parameter_store_test.go
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package data | ||
|
||
import ( | ||
"context" | ||
"github.com/aws/aws-sdk-go-v2/service/ssm" | ||
"strings" | ||
) | ||
|
||
type SSMClient interface { | ||
PutParameter(ctx context.Context, params *ssm.PutParameterInput, optFns ...func(*ssm.Options)) (*ssm.PutParameterOutput, error) | ||
GetParameter(ctx context.Context, params *ssm.GetParameterInput, optFns ...func(*ssm.Options)) (*ssm.GetParameterOutput, error) | ||
DeleteParameter(ctx context.Context, params *ssm.DeleteParameterInput, optFns ...func(*ssm.Options)) (*ssm.DeleteParameterOutput, error) | ||
} | ||
|
||
type SSMConnection struct { | ||
Prefix string | ||
Client SSMClient | ||
} | ||
|
||
func NewSSMConnection(client SSMClient, prefix string) *SSMConnection { | ||
return &SSMConnection{Client: client, Prefix: prefix} | ||
} | ||
|
||
func (s *SSMConnection) prefixedParameterName(name string) string { | ||
// if no prefix, then return unchanged | ||
if s.Prefix == "" { | ||
return name | ||
} | ||
|
||
// if or no / to insert prefix next to, then return unchanged | ||
if !strings.Contains(name[1:], "/") { | ||
return name | ||
} | ||
|
||
i := strings.Index(name[1:], "/") | ||
|
||
x := name[:i+1] + "/" + s.Prefix + "/" + strings.TrimLeft(name[i+1:], "/") | ||
|
||
return x | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package data_test | ||
|
||
import ( | ||
"context" | ||
"github.com/aws/aws-sdk-go-v2/service/ssm" | ||
"github.com/ministryofjustice/opg-use-an-lpa/service-admin/internal/server/data" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
type MockSSMClient struct{} | ||
|
||
func (m MockSSMClient) PutParameter(ctx context.Context, params *ssm.PutParameterInput, optFns ...func(*ssm.Options)) (*ssm.PutParameterOutput, error) { | ||
return nil, nil | ||
} | ||
|
||
func (m MockSSMClient) GetParameter(ctx context.Context, params *ssm.GetParameterInput, optFns ...func(*ssm.Options)) (*ssm.GetParameterOutput, error) { | ||
return nil, nil | ||
} | ||
|
||
func (m MockSSMClient) DeleteParameter(ctx context.Context, params *ssm.DeleteParameterInput, optFns ...func(*ssm.Options)) (*ssm.DeleteParameterOutput, error) { | ||
return nil, nil | ||
} | ||
|
||
// test for new method, which takes in ssmclient | ||
func TestNewSSMConnection(t *testing.T) { | ||
t.Parallel() | ||
|
||
got := data.NewSSMConnection(&MockSSMClient{}, "") | ||
assert.NotNil(t, got) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package data | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/ssm" | ||
"github.com/aws/aws-sdk-go-v2/service/ssm/types" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
type SystemMessageService struct { | ||
ssmConn SSMConnection | ||
} | ||
|
||
func NewSystemMessageService(ssmConn SSMConnection) *SystemMessageService { | ||
return &SystemMessageService{ssmConn: ssmConn} | ||
} | ||
|
||
func (s *SystemMessageService) GetSystemMessages(ctx context.Context) (map[string]string, error) { | ||
messageKeys := []string{"/system-message/use/en", "/system-message/use/cy", "/system-message/view/en", "/system-message/view/cy"} | ||
messages := make(map[string]string) | ||
|
||
for _, messageKey := range messageKeys { | ||
messageText, err := s.ssmConn.Client.GetParameter(ctx, &ssm.GetParameterInput{ | ||
Name: aws.String(s.ssmConn.prefixedParameterName(messageKey)), | ||
WithDecryption: aws.Bool(true), | ||
}) | ||
if err != nil { | ||
log.Error().Err(err).Msg(fmt.Sprintf("error retrieving parameter: %s", messageKey)) | ||
continue | ||
} | ||
|
||
if messageText != nil && messageText.Parameter != nil && messageText.Parameter.Value != nil { | ||
messages[messageKey] = *messageText.Parameter.Value | ||
} | ||
} | ||
|
||
return messages, nil | ||
} | ||
|
||
func (s *SystemMessageService) PutSystemMessages(ctx context.Context, messages map[string]string) (bool, error) { | ||
deleted := false | ||
|
||
for messageKey, messageValue := range messages { | ||
if messageValue != "" { | ||
_, err := s.ssmConn.Client.PutParameter(ctx, &ssm.PutParameterInput{ | ||
Name: aws.String(s.ssmConn.prefixedParameterName(messageKey)), | ||
Value: aws.String(messageValue), | ||
Type: types.ParameterTypeString, | ||
Overwrite: aws.Bool(true), | ||
}) | ||
if err != nil { | ||
return false, fmt.Errorf("error writing parameter: %w", err) | ||
} | ||
} else { | ||
_, err := s.ssmConn.Client.DeleteParameter(ctx, &ssm.DeleteParameterInput{ | ||
Name: aws.String(s.ssmConn.prefixedParameterName(messageKey)), | ||
}) | ||
if err != nil { | ||
var pnf *types.ParameterNotFound | ||
if errors.As(err, &pnf) { | ||
log.Debug().Msg(fmt.Sprintf("not deleting parameter '%s' as it does not exist", messageKey)) | ||
} else { | ||
return false, fmt.Errorf("error deleting parameter: %w", err) | ||
} | ||
} else { | ||
deleted = true | ||
} | ||
} | ||
} | ||
|
||
return deleted, nil | ||
} |
Oops, something went wrong.