-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
172 lines (147 loc) · 3.89 KB
/
string.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
Copyright © 2023 MicroOps-cn.
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 safe
import (
"database/sql/driver"
"encoding/json"
"fmt"
"os"
"strings"
"github.com/gogo/protobuf/jsonpb"
"gopkg.in/yaml.v3"
)
type String struct {
value string
secret string
}
func (e *String) Reset() { *e = String{} }
func (e *String) getSecret() string {
if e.secret != "" {
return e.secret
}
return os.Getenv(SecretEnvName)
}
func (e *String) String() string {
if !strings.HasPrefix(e.value, ciphertextPrefix) {
if secret := e.getSecret(); secret != "" {
if value, err := Encrypt([]byte(e.value), e.secret, nil); err == nil {
e.value = value
}
}
}
return e.value
}
func (e *String) XXX_WellKnownType() string { return "StringValue" } //nolint:revive
func (e *String) ProtoMessage() {}
func (e *String) UnmarshalJSONPB(_ *jsonpb.Unmarshaler, bytes []byte) error {
return e.UnmarshalJSON(bytes)
}
func (e String) MarshalJSONPB(_ *jsonpb.Marshaler) ([]byte, error) {
return e.MarshalJSON()
}
func (e String) MarshalJSON() ([]byte, error) {
return json.Marshal(e.String())
}
func (e *String) UnmarshalJSON(bytes []byte) (err error) {
if err = json.Unmarshal(bytes, &e.value); err != nil {
return err
}
if !strings.HasPrefix(e.value, ciphertextPrefix) {
if secret := e.getSecret(); secret != "" {
if e.value, err = Encrypt([]byte(e.value), secret, nil); err != nil {
return err
}
e.secret = secret
}
}
return nil
}
func (e *String) UnmarshalYAML(value *yaml.Node) (err error) {
if err := value.Decode(&e.value); err != nil {
return err
}
if !strings.HasPrefix(e.value, ciphertextPrefix) {
if secret := e.getSecret(); secret != "" {
if e.value, err = Encrypt([]byte(e.value), secret, nil); err != nil {
return err
}
e.secret = secret
}
}
return nil
}
func (e String) UnsafeString() (string, error) {
if strings.HasPrefix(e.value, ciphertextPrefix) {
fmt.Println(e.getSecret(), e.value)
if secret := e.getSecret(); secret != "" {
decrypt, err := Decrypt(e.value, secret)
return string(decrypt), err
}
}
return e.value, nil
}
func (e String) Size() int {
return len(e.value)
}
func (*String) GormDataType() string {
return "string"
}
func (e *String) SetValue(value string) {
if !strings.HasPrefix(value, ciphertextPrefix) {
if secret := e.getSecret(); secret != "" {
if safeString, err := Encrypt([]byte(value), e.secret, nil); err == nil {
e.value = safeString
}
}
} else {
e.value = value
}
}
func (e *String) SetSecret(secret string) {
plain, err := e.UnsafeString()
if err != nil {
return
}
if len(secret) != 0 {
if safeString, err := Encrypt([]byte(plain), secret, nil); err == nil {
e.value = safeString
}
}
e.secret = secret
}
// Scan implements the Scanner interface.
func (e *String) Scan(value any) error {
switch vt := value.(type) {
case []uint8:
e.value = string(vt)
case string:
e.value = vt
default:
return fmt.Errorf("failed to resolve field, type exception: %T", value)
}
return nil
}
// Value implements the driver Valuer interface.
func (e String) Value() (driver.Value, error) {
return e.value, nil
}
func NewEncryptedString(plain, secret string) *String {
if !strings.HasPrefix(plain, ciphertextPrefix) {
if len(secret) > 0 {
if safeString, err := Encrypt([]byte(plain), secret, nil); err == nil {
return &String{value: safeString, secret: secret}
}
}
}
return &String{value: plain, secret: secret}
}