@@ -14,19 +14,229 @@ function aws_secretmanager_list() {
14
14
"
15
15
}
16
16
17
- function aws_secretmanager_get_value() {
17
+ function aws_secretmanager_get() {
18
+ local secret_name=$1
19
+
20
+ # Check input invalid
21
+ if [[ -z " $secret_name " ]]; then return ; fi
22
+ aws_run_commandline " \
23
+ aws secretsmanager describe-secret --secret-id '${secret_name} '
24
+ "
25
+ }
26
+
27
+ function aws_secretmanager_get_with_hint() {
28
+ local secret_name=$( peco_create_menu ' peco_aws_secretmanager_list' ' --prompt "Choose secret that you want >"' )
29
+
30
+ # Check input invalid
31
+ if [[ -z " $secret_name " ]]; then return ; fi
32
+ aws_secretmanager_get " ${secret_name} "
33
+ }
18
34
35
+ function aws_secretmanager_get_value() {
19
36
local secret_name=$1
37
+ local nolog=${2:- ' no' }
20
38
# Check input invalid
21
39
if [[ -z " $secret_name " ]]; then return ; fi
22
40
23
- aws_run_commandline " \
41
+ if [[ " $nolog " = " yes" ]]; then
42
+ aws secretsmanager get-secret-value \
43
+ --secret-id " ${secret_name} "
44
+ else
45
+
46
+ aws_run_commandline " \
24
47
aws secretsmanager get-secret-value \
25
48
--secret-id " ${secret_name} "
26
49
"
50
+
51
+ fi
52
+
27
53
}
28
54
29
55
function aws_secretmanager_get_value_with_hint() {
30
56
local secret_name=$( peco_create_menu ' peco_aws_secretmanager_list' ' --prompt "Choose secret that you want >"' )
31
57
aws_secretmanager_get_value " ${secret_name} "
32
58
}
59
+
60
+ function aws_secretmanager_get_value_with_specific_key_with_hint() {
61
+ local secret_name=$( peco_create_menu ' peco_aws_secretmanager_list' ' --prompt "Choose secret that you want >"' )
62
+ aws_secretmanager_get_value_with_specific_key " ${secret_name} " " "
63
+
64
+ }
65
+
66
+ function aws_secretmanager_list_keys() {
67
+ local secret_name=" $1 "
68
+
69
+ # Validate input
70
+ if [[ -z " $secret_name " ]]; then
71
+ echo " Usage: aws_secretmanager_list_keys <secret-name>"
72
+ return 1
73
+ fi
74
+
75
+ # Fetch the secret value
76
+ local secret_json
77
+ secret_json=$( aws secretsmanager get-secret-value --secret-id " $secret_name " --query SecretString --output text 2> /dev/null)
78
+
79
+ # Check if secret retrieval was successful
80
+ if [[ -z " $secret_json " ]]; then
81
+ echo " Failed to retrieve secret or secret is empty."
82
+ return 1
83
+ fi
84
+
85
+ # List all keys using jq
86
+ echo " $secret_json " | jq -r ' keys[]'
87
+ }
88
+
89
+ function aws_secretmanager_list_keys_with_hint() {
90
+ local secret_name=$( peco_create_menu ' peco_aws_secretmanager_list' ' --prompt "Choose secret that you want >"' )
91
+
92
+ echo " Keys in secret '$secret_name ':"
93
+ aws_secretmanager_list_keys " ${secret_name} "
94
+ }
95
+
96
+ function aws_secretmanager_get_value_with_specific_key() {
97
+
98
+ local secret_name=$1
99
+ # No log to file
100
+ secret_string=$( aws_secretmanager_get_value " ${secret_name} " " yes" | jq ' .SecretString' )
101
+ secret_string_json=$( python3 -c " import sys,json; print(json.loads(sys.argv[1]))" " ${secret_string} " | jq)
102
+ local secret_keys=$( echo " ${secret_string_json} " | jq -r ' keys_unsorted[]' )
103
+
104
+ local secret_key=$( peco_create_menu ' echo ${secret_keys}' ' --prompt "Choose secret key that you want get value>"' )
105
+
106
+ # Check input invalid
107
+ if [[ -z " $secret_key " ]]; then
108
+ echo " The secret key is invalid"
109
+ return
110
+ fi
111
+
112
+ local secret_value=$( echo " ${secret_string_json} " | jq -r " .${secret_key} " )
113
+ echo " ${secret_key} =${secret_value} "
114
+
115
+ }
116
+
117
+ function aws_secretmanager_update_secret() {
118
+ local secret_name=" $1 "
119
+ local secret_key=" $2 "
120
+ local secret_value=" $3 "
121
+ local existing_secret_json
122
+ local updated_secret_json
123
+
124
+ # Check input invalid
125
+ if [[ -z " $secret_name " ]]; then return ; fi
126
+ if [[ -z " $secret_key " ]]; then return ; fi
127
+ if [[ -z " $secret_value " ]]; then return ; fi
128
+
129
+ # Fetch the existing secret value
130
+ existing_secret_json=$( aws secretsmanager get-secret-value --secret-id " $secret_name " --query SecretString --output text 2> /dev/null || echo " " )
131
+
132
+ # Check if the secret exists
133
+ if [[ " $existing_secret_json " == " " ]]; then
134
+ echo " Secret '$secret_name ' not found or has no existing JSON data. Creating a new secret."
135
+
136
+ # shellcheck disable=SC2155
137
+ local updated_secret_json=$(
138
+ cat << -__EOF__
139
+ {
140
+ "${secret_name} ": "${secret_value} "
141
+ }
142
+ __EOF__
143
+ )
144
+
145
+ echo " $lhs_docs "
146
+ else
147
+ # Update the JSON secret by modifying the key-value pair
148
+ echo " \nAppend to the exsting json"
149
+ updated_secret_json=$( echo " $existing_secret_json " | jq --arg key " $secret_key " --arg value " $secret_value " ' .[$key] = $value' )
150
+ fi
151
+
152
+ # Update the secret in AWS Secrets Manager
153
+ aws secretsmanager put-secret-value --secret-id " $secret_name " --secret-string " $updated_secret_json "
154
+
155
+ echo " Secret '$secret_name ' updated with key ${secret_key} successfully."
156
+ }
157
+
158
+ function aws_secretmanager_update_specific_secret_key_with_hint() {
159
+
160
+ local secret_name=$( peco_create_menu ' peco_aws_secretmanager_list' ' --prompt "Choose secret that you want >"' )
161
+
162
+ # Check input invalid
163
+ if [[ -z " $secret_name " ]]; then
164
+ echo " Secret name is invalid. "
165
+ return
166
+ fi
167
+
168
+ echo " List existing keys"
169
+ aws_secretmanager_list_keys " ${secret_name} "
170
+
171
+ while true ; do
172
+ echo -n " Enter name of secret key (or press Enter to finish): "
173
+ read secret_key
174
+
175
+ # Break loop if user presses Enter without input
176
+ if [[ -z " $secret_key " ]]; then
177
+ break
178
+ fi
179
+
180
+ echo -n " Enter value for '${secret_key} ': "
181
+ read -s secret_value
182
+ echo
183
+
184
+ # Validate secret value
185
+ if [[ -z " $secret_value " ]]; then
186
+ echo " Secret value cannot be empty."
187
+ continue
188
+ fi
189
+
190
+ aws_secretmanager_update_secret " ${secret_name} " " ${secret_key} " " ${secret_value} "
191
+
192
+ done
193
+
194
+ }
195
+
196
+ function aws_secretmanager_delete_key() { # Be careful when using this
197
+ local secret_name=" $1 "
198
+ local key_to_delete=" $2 "
199
+
200
+ # Validate inputs
201
+ if [[ -z " $secret_name " || -z " $key_to_delete " ]]; then
202
+ echo " Usage: aws_secretmanager_delete_key <secret-name> <key-to-delete>"
203
+ return 1
204
+ fi
205
+
206
+ # Fetch the existing secret value
207
+ local existing_secret_json
208
+ existing_secret_json=$( aws secretsmanager get-secret-value --secret-id " $secret_name " --query SecretString --output text 2> /dev/null)
209
+
210
+ # Validate secret retrieval
211
+ if [[ -z " $existing_secret_json " ]]; then
212
+ echo " Secret '$secret_name ' not found or has no existing data."
213
+ return 1
214
+ fi
215
+
216
+ # Check if key exists
217
+ if ! echo " $existing_secret_json " | jq -e --arg key " $key_to_delete " ' has($key)' > /dev/null; then
218
+ echo " Key '$key_to_delete ' not found in secret '$secret_name '."
219
+ return 1
220
+ fi
221
+
222
+ # Remove the key from the JSON
223
+ local updated_secret_json
224
+ updated_secret_json=$( echo " $existing_secret_json " | jq " del(.\" $key_to_delete \" )" )
225
+
226
+ # Update the secret in AWS Secrets Manager
227
+ aws secretsmanager put-secret-value --secret-id " $secret_name " --secret-string " $updated_secret_json "
228
+
229
+ echo " Key '$key_to_delete ' has been removed from secret '$secret_name '."
230
+ }
231
+
232
+ function aws_secretmanager_delete_key_with_hint() { # # Be careful when using this
233
+ local secret_name=$( peco_create_menu ' peco_aws_secretmanager_list' ' --prompt "Choose secret that you want >"' )
234
+
235
+ # Check input invalid
236
+ if [[ -z " $secret_name " ]]; then return ; fi
237
+
238
+ local secret_key=$( peco_create_menu ' aws_secretmanager_list_keys ${secret_name}' ' --prompt "Choose secret key that you want >"' )
239
+
240
+ aws_secretmanager_delete_key " ${secret_name} " ${secret_key}
241
+
242
+ }
0 commit comments