From 2bee6ef91b38be8fb449e10705b57b8a6c9f8112 Mon Sep 17 00:00:00 2001 From: weichou Date: Wed, 26 May 2021 16:01:29 +0800 Subject: [PATCH] feat: Add secret route path and HTTP client Add secret route path and HTTP client for adding EdgeX Service exclusive secret to the Secret Store. Close #623 Signed-off-by: weichou --- v2/clients/http/common.go | 10 ++++++++- v2/clients/http/common_test.go | 20 ++++++++++++++++- v2/clients/interfaces/common.go | 4 +++- v2/clients/interfaces/mocks/CommonClient.go | 25 ++++++++++++++++++++- v2/constants.go | 1 + v2/dtos/common/secret.go | 8 +++++++ 6 files changed, 64 insertions(+), 4 deletions(-) diff --git a/v2/clients/http/common.go b/v2/clients/http/common.go index f6021fb8..80e0fa59 100644 --- a/v2/clients/http/common.go +++ b/v2/clients/http/common.go @@ -1,5 +1,5 @@ // -// Copyright (C) 2020 IOTech Ltd +// Copyright (C) 2020-2021 IOTech Ltd // // SPDX-License-Identifier: Apache-2.0 @@ -61,3 +61,11 @@ func (cc *commonClient) Version(ctx context.Context) (common.VersionResponse, er } return vr, nil } + +func (cc *commonClient) AddSecret(ctx context.Context, request common.SecretRequest) (res common.BaseResponse, err errors.EdgeX) { + err = utils.PostRequestWithRawData(ctx, &res, cc.baseUrl+v2.ApiSecretRoute, request) + if err != nil { + return res, errors.NewCommonEdgeXWrapper(err) + } + return res, nil +} diff --git a/v2/clients/http/common_test.go b/v2/clients/http/common_test.go index e39d3284..a9e757a1 100644 --- a/v2/clients/http/common_test.go +++ b/v2/clients/http/common_test.go @@ -1,5 +1,5 @@ // -// Copyright (C) 2020 IOTech Ltd +// Copyright (C) 2020-2021 IOTech Ltd // // SPDX-License-Identifier: Apache-2.0 @@ -65,6 +65,24 @@ func TestVersion(t *testing.T) { require.Equal(t, expected, response) } +func TestAddSecret(t *testing.T) { + expected := common.BaseResponse{} + req := common.NewSecretRequest( + "testPath", + []common.SecretDataKeyValue{ + {Key: "username", Value: "tester"}, + {Key: "password", Value: "123"}, + }, + ) + ts := newTestServer(http.MethodPost, v2.ApiSecretRoute, expected) + defer ts.Close() + + client := NewCommonClient(ts.URL) + res, err := client.AddSecret(context.Background(), req) + require.NoError(t, err) + require.IsType(t, expected, res) +} + func newTestServer(httpMethod string, apiRoute string, expectedResponse interface{}) *httptest.Server { return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != httpMethod { diff --git a/v2/clients/interfaces/common.go b/v2/clients/interfaces/common.go index f1d5548d..13c1196d 100644 --- a/v2/clients/interfaces/common.go +++ b/v2/clients/interfaces/common.go @@ -1,5 +1,5 @@ // -// Copyright (C) 2020 IOTech Ltd +// Copyright (C) 2020-2021 IOTech Ltd // // SPDX-License-Identifier: Apache-2.0 @@ -21,4 +21,6 @@ type CommonClient interface { Ping(ctx context.Context) (common.PingResponse, errors.EdgeX) // Version obtains version information from the target service. Version(ctx context.Context) (common.VersionResponse, errors.EdgeX) + // AddSecret adds EdgeX Service exclusive secret to the Secret Store + AddSecret(ctx context.Context, request common.SecretRequest) (common.BaseResponse, errors.EdgeX) } diff --git a/v2/clients/interfaces/mocks/CommonClient.go b/v2/clients/interfaces/mocks/CommonClient.go index 7afa8589..74933b1a 100644 --- a/v2/clients/interfaces/mocks/CommonClient.go +++ b/v2/clients/interfaces/mocks/CommonClient.go @@ -1,4 +1,4 @@ -// Code generated by mockery v2.5.1. DO NOT EDIT. +// Code generated by mockery v2.7.4. DO NOT EDIT. package mocks @@ -17,6 +17,29 @@ type CommonClient struct { mock.Mock } +// AddSecret provides a mock function with given fields: ctx, request +func (_m *CommonClient) AddSecret(ctx context.Context, request common.SecretRequest) (common.BaseResponse, errors.EdgeX) { + ret := _m.Called(ctx, request) + + var r0 common.BaseResponse + if rf, ok := ret.Get(0).(func(context.Context, common.SecretRequest) common.BaseResponse); ok { + r0 = rf(ctx, request) + } else { + r0 = ret.Get(0).(common.BaseResponse) + } + + var r1 errors.EdgeX + if rf, ok := ret.Get(1).(func(context.Context, common.SecretRequest) errors.EdgeX); ok { + r1 = rf(ctx, request) + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).(errors.EdgeX) + } + } + + return r0, r1 +} + // Configuration provides a mock function with given fields: ctx func (_m *CommonClient) Configuration(ctx context.Context) (common.ConfigResponse, errors.EdgeX) { ret := _m.Called(ctx) diff --git a/v2/constants.go b/v2/constants.go index 6ed0e97b..52684eca 100644 --- a/v2/constants.go +++ b/v2/constants.go @@ -94,6 +94,7 @@ const ( ApiMetricsRoute = ApiBase + "/metrics" ApiPingRoute = ApiBase + "/ping" ApiVersionRoute = ApiBase + "/version" + ApiSecretRoute = ApiBase + "/secret" ApiDeviceCallbackRoute = ApiBase + "/callback/device" ApiDeviceCallbackNameRoute = ApiBase + "/callback/device/name/{name}" diff --git a/v2/dtos/common/secret.go b/v2/dtos/common/secret.go index 6e20070c..0fa8ff96 100644 --- a/v2/dtos/common/secret.go +++ b/v2/dtos/common/secret.go @@ -64,3 +64,11 @@ func (sr *SecretRequest) UnmarshalJSON(b []byte) error { } return nil } + +func NewSecretRequest(path string, secretData []SecretDataKeyValue) SecretRequest { + return SecretRequest{ + BaseRequest: NewBaseRequest(), + Path: path, + SecretData: secretData, + } +}