Skip to content

Commit d73d440

Browse files
authored
Merge pull request #981 from lindseysimple/issue-980
feat: Add the SecretStoreTokenClient http client
2 parents 62fd26d + c8bd699 commit d73d440

File tree

5 files changed

+175
-0
lines changed

5 files changed

+175
-0
lines changed

clients/http/secretstoretoken.go

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// Copyright (C) 2025 IOTech Ltd
3+
//
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
package http
7+
8+
import (
9+
"context"
10+
11+
"github.com/edgexfoundry/go-mod-core-contracts/v4/clients"
12+
"github.com/edgexfoundry/go-mod-core-contracts/v4/clients/http/utils"
13+
"github.com/edgexfoundry/go-mod-core-contracts/v4/clients/interfaces"
14+
"github.com/edgexfoundry/go-mod-core-contracts/v4/common"
15+
dtoCommon "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/common"
16+
"github.com/edgexfoundry/go-mod-core-contracts/v4/errors"
17+
)
18+
19+
type SecretStoreTokenClient struct {
20+
baseUrlFunc clients.ClientBaseUrlFunc
21+
authInjector interfaces.AuthenticationInjector
22+
}
23+
24+
// NewSecretStoreTokenClient creates an instance of SecretStoreTokenClient
25+
func NewSecretStoreTokenClient(baseUrl string, authInjector interfaces.AuthenticationInjector) interfaces.SecretStoreTokenClient {
26+
return &SecretStoreTokenClient{
27+
baseUrlFunc: clients.GetDefaultClientBaseUrlFunc(baseUrl),
28+
authInjector: authInjector,
29+
}
30+
}
31+
32+
// NewSecretStoreTokenClientWithUrlCallback creates an instance of SecretStoreTokenClient with ClientBaseUrlFunc.
33+
func NewSecretStoreTokenClientWithUrlCallback(baseUrlFunc clients.ClientBaseUrlFunc, authInjector interfaces.AuthenticationInjector) interfaces.AuthClient {
34+
return &AuthClient{
35+
baseUrlFunc: baseUrlFunc,
36+
authInjector: authInjector,
37+
}
38+
}
39+
40+
// RegenToken regenerates the secret store client token based on the specified entity id
41+
func (ac *SecretStoreTokenClient) RegenToken(ctx context.Context, entityId string) (dtoCommon.BaseResponse, errors.EdgeX) {
42+
var response dtoCommon.BaseResponse
43+
baseUrl, err := clients.GetBaseUrl(ac.baseUrlFunc)
44+
if err != nil {
45+
return response, errors.NewCommonEdgeXWrapper(err)
46+
}
47+
48+
path := common.NewPathBuilder().SetPath(common.ApiTokenRoute).SetPath(common.EntityId).SetPath(entityId).BuildPath()
49+
err = utils.PutRequest(ctx, &response, baseUrl, path, nil, nil, ac.authInjector)
50+
if err != nil {
51+
return response, errors.NewCommonEdgeXWrapper(err)
52+
}
53+
return response, nil
54+
}

clients/http/secretstoretoken_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// Copyright (C) 2025 IOTech Ltd
3+
//
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
package http
7+
8+
import (
9+
"context"
10+
"net/http"
11+
"testing"
12+
13+
"github.com/edgexfoundry/go-mod-core-contracts/v4/common"
14+
dtoCommon "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/common"
15+
16+
"github.com/stretchr/testify/require"
17+
)
18+
19+
func TestRegenToken(t *testing.T) {
20+
mockId := "mockId"
21+
22+
path := common.NewPathBuilder().EnableNameFieldEscape(false).
23+
SetPath(common.ApiTokenRoute).SetPath(common.EntityId).SetPath(mockId).BuildPath()
24+
ts := newTestServer(http.MethodPut, path, dtoCommon.BaseResponse{})
25+
defer ts.Close()
26+
27+
client := NewSecretStoreTokenClient(ts.URL, NewNullAuthenticationInjector())
28+
res, err := client.RegenToken(context.Background(), mockId)
29+
require.NoError(t, err)
30+
require.IsType(t, dtoCommon.BaseResponse{}, res)
31+
}

clients/interfaces/mocks/SecretStoreTokenClient.go

+62
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// Copyright (C) 2025 IOTech Ltd
3+
//
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
package interfaces
7+
8+
import (
9+
"context"
10+
11+
dtoCommon "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/common"
12+
"github.com/edgexfoundry/go-mod-core-contracts/v4/errors"
13+
)
14+
15+
// SecretStoreTokenClient defines the interface for interactions with the API endpoint on the security-secretstore-setup service.
16+
type SecretStoreTokenClient interface {
17+
// RegenToken regenerates the secret store client token based on the specified entity id
18+
RegenToken(ctx context.Context, entityId string) (dtoCommon.BaseResponse, errors.EdgeX)
19+
}

common/constants.go

+9
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ const (
136136

137137
ApiKeyRoute = ApiBase + "/key"
138138
ApiVerificationKeyByIssuerRoute = ApiKeyRoute + "/" + VerificationKeyType + "/" + Issuer + "/:" + Issuer
139+
140+
ApiTokenRoute = ApiBase + "/" + Token
141+
ApiRegenTokenRoute = ApiTokenRoute + "/" + EntityId + "/:" + EntityId
139142
)
140143

141144
// Constants related to defined url path names and parameters in the v3 service APIs
@@ -405,3 +408,9 @@ const (
405408
SigningKeyType = "signing"
406409
Issuer = "issuer"
407410
)
411+
412+
// Constants related to the security-secretstore-setup service
413+
const (
414+
EntityId = "entityId"
415+
Token = "token"
416+
)

0 commit comments

Comments
 (0)