Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
745c0c94
Unverified
Commit
745c0c94
authored
1 year ago
by
metamben
Committed by
GitHub
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Read the latest version of the secret from the DB (#34335)
parent
a597d5ef
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/metabase/models/secret.clj
+10
-10
10 additions, 10 deletions
src/metabase/models/secret.clj
test/metabase/models/secret_test.clj
+41
-0
41 additions, 0 deletions
test/metabase/models/secret_test.clj
with
51 additions
and
10 deletions
src/metabase/models/secret.clj
+
10
−
10
View file @
745c0c94
...
...
@@ -143,6 +143,12 @@
"Regex for parsing base64 encoded file uploads."
#
"^data:application/([^;]*);base64,"
)
(
defn
latest-for-id
"Returns the latest Secret instance for the given `id` (meaning the one with the highest `version`)."
{
:added
"0.42.0"
}
[
id
]
(
t2/select-one
Secret
:id
id
{
:order-by
[[
:version
:desc
]]}))
(
defn
db-details-prop->secret-map
"Returns a map containing `:value` and `:source` for the given `conn-prop-nm`. `conn-prop-nm` is expected to be the
name of a connection property having `:type` `:secret`, and the relevant sub-properties (ex: -value, -path, etc.) will
...
...
@@ -185,14 +191,14 @@
{
:invalid-db-details-entry
(
select-keys
details
[
path-kw
])}))))
(
id-kw
details
)
(
:value
(
t2/select-one
Secret
:
id
(
id-kw
details
))))
(
:value
(
latest-for-
id
(
id-kw
details
))))
source
(
cond
;; set the :source due to the -path suffix (see above))
(
and
(
not=
"uploaded"
(
options-kw
details
))
(
path-kw
details
))
:file-path
(
id-kw
details
)
(
:source
(
t2/select-one
Secret
:
id
(
id-kw
details
))))]
(
:source
(
latest-for-
id
(
id-kw
details
))))]
(
cond->
{
:connection-property-name
conn-prop-nm,
:subprops
[
path-kw
value-kw
id-kw
]}
value
(
assoc
:value
value
...
...
@@ -209,7 +215,7 @@
value
(
String.
^
bytes
value
"UTF-8"
)))
(
when
id
(
String.
^
bytes
(
:value
(
t2/select-one
Secret
:
id
id
))
"UTF-8"
)))]
(
String.
^
bytes
(
:value
(
latest-for-
id
id
))
"UTF-8"
)))]
(
case
(
options-kw
details
)
"uploaded"
(
try
;; When a secret is updated, the value has already been decoded
...
...
@@ -226,12 +232,6 @@
bump-version-keys
[
:kind
:source
:value
])
(
defn
latest-for-id
"Returns the latest Secret instance for the given `id` (meaning the one with the highest `version`)."
{
:added
"0.42.0"
}
[
id
]
(
t2/select-one
Secret
:id
id
{
:order-by
[[
:version
:desc
]]}))
(
defn
upsert-secret-value!
"Inserts a new secret value, or updates an existing one, for the given parameters.
* if there is no existing Secret instance, inserts with the given field values
...
...
@@ -305,7 +305,7 @@
(
let
[
subprop
(
fn
[
prop-nm
]
(
keyword
(
str
conn-prop-nm
prop-nm
)))
secret*
(
cond
(
int?
secret-or-id
)
(
t2/select-one
Secret
:
id
secret-or-id
)
(
latest-for-
id
secret-or-id
)
(
mi/instance-of?
Secret
secret-or-id
)
secret-or-id
...
...
This diff is collapsed.
Click to expand it.
test/metabase/models/secret_test.clj
+
41
−
0
View file @
745c0c94
...
...
@@ -210,3 +210,44 @@
(
is
(
bytes?
decoded
))
(
is
(
=
content
(
String.
^
bytes
decoded
"UTF-8"
))))))))))
(
deftest
use-latest-version-test
(
testing
"when reading a secret from the DB, the latest version is taken (#33116)"
(
let
[
initial-value
"vnetillo"
latest-value
(
str
initial-value
"s"
)
initial-source
"source"
latest-source
(
str
initial-source
"s"
)
secret-property
"secret"
secret-map1
{
:name
secret-property
:kind
::secret/password
:value
initial-value
:source
initial-source
}
secret-map2
{
:name
secret-property
:kind
::secret/password
:value
latest-value
:source
latest-source
}
crowberto-id
(
mt/user->id
:crowberto
)
by-crowberto
#
(
assoc
%
:creator_id
crowberto-id
)
timestamp-string?
(
fn
[
s
]
(
try
(
java.time.Instant/parse
s
)
true
(
catch
Exception
_
false
)))]
(
t2.with-temp/with-temp
[
Secret
{
:keys
[
id
version
]}
(
by-crowberto
secret-map1
)
Secret
_
(
->
secret-map2
by-crowberto
(
assoc
:id
id
:version
(
inc
version
)))]
(
let
[
details
{
:secret-id
id
}]
(
testing
"db-details-prop->secret-map"
(
let
[
secret
(
secret/db-details-prop->secret-map
details
secret-property
)]
(
is
(
=
latest-value
(
->
secret
:value
bytes
(
String.
"UTF8"
))))
(
is
(
=
(
keyword
latest-source
)
(
:source
secret
)))))
(
testing
"get-secret-string"
(
is
(
=
latest-value
(
secret/get-secret-string
details
secret-property
))))
(
testing
"expand-inferred-secret-values"
(
is
(
=?
{
:secret-id
id
:secret-source
(
keyword
latest-source
)
:secret-creator-id
crowberto-id
:secret-created-at
timestamp-string?
}
(
secret/expand-inferred-secret-values
details
secret-property
id
)))))))))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment