-
Notifications
You must be signed in to change notification settings - Fork 54
/
types.go
69 lines (56 loc) · 2.44 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*******************************************************************************
* Copyright 2021 Intel Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*******************************************************************************/
package secret
import (
"encoding/json"
"fmt"
validation "github.com/edgexfoundry/go-mod-core-contracts/v2/common"
"github.com/edgexfoundry/go-mod-core-contracts/v2/dtos/common"
"github.com/hashicorp/go-multierror"
)
// ServiceSecrets contains the list of secrets to import into a service's SecretStore
type ServiceSecrets struct {
Secrets []ServiceSecret `json:"secrets" validate:"required,gt=0,dive"`
}
// ServiceSecret contains the information about a service's secret to import into a service's SecretStore
type ServiceSecret struct {
Path string `json:"path" validate:"edgex-dto-none-empty-string"`
Imported bool `json:"imported"`
SecretData []common.SecretDataKeyValue `json:"secretData" validate:"required,dive"`
}
// MarshalJson marshal the service's secrets to JSON.
func (s *ServiceSecrets) MarshalJson() ([]byte, error) {
return json.Marshal(s)
}
// UnmarshalServiceSecretsJson un-marshals the JSON containing the services list of secrets
func UnmarshalServiceSecretsJson(data []byte) (*ServiceSecrets, error) {
secrets := &ServiceSecrets{}
if err := json.Unmarshal(data, secrets); err != nil {
return nil, err
}
if err := validation.Validate(secrets); err != nil {
return nil, err
}
var validationErrs error
// Since secretData len validation can't be specified to only validate when Imported=false, we have to do it manually here
for _, secret := range secrets.Secrets {
if !secret.Imported && len(secret.SecretData) == 0 {
validationErrs = multierror.Append(validationErrs, fmt.Errorf("SecretData for '%s' must not be empty when Imported=false", secret.Path))
}
}
if validationErrs != nil {
return nil, validationErrs
}
return secrets, nil
}