Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
1914a1e3
Commit
1914a1e3
authored
Jun 10, 2021
by
Gosia Ksionek
Committed by
Sean McGivern
Jun 10, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add new way of encrypting tokens
Changelog: changed
parent
9258c973
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
100 additions
and
14 deletions
+100
-14
app/models/concerns/token_authenticatable_strategies/encryption_helper.rb
...rns/token_authenticatable_strategies/encryption_helper.rb
+8
-4
config/feature_flags/ops/dynamic_nonce.yml
config/feature_flags/ops/dynamic_nonce.yml
+8
-0
spec/models/concerns/token_authenticatable_strategies/encryption_helper_spec.rb
...oken_authenticatable_strategies/encryption_helper_spec.rb
+84
-10
No files found.
app/models/concerns/token_authenticatable_strategies/encryption_helper.rb
View file @
1914a1e3
...
@@ -5,10 +5,6 @@ module TokenAuthenticatableStrategies
...
@@ -5,10 +5,6 @@ module TokenAuthenticatableStrategies
DYNAMIC_NONCE_IDENTIFIER
=
"|"
DYNAMIC_NONCE_IDENTIFIER
=
"|"
NONCE_SIZE
=
12
NONCE_SIZE
=
12
def
self
.
encrypt_token
(
plaintext_token
)
Gitlab
::
CryptoHelper
.
aes256_gcm_encrypt
(
plaintext_token
)
end
def
self
.
decrypt_token
(
token
)
def
self
.
decrypt_token
(
token
)
return
unless
token
return
unless
token
...
@@ -22,5 +18,13 @@ module TokenAuthenticatableStrategies
...
@@ -22,5 +18,13 @@ module TokenAuthenticatableStrategies
Gitlab
::
CryptoHelper
.
aes256_gcm_decrypt
(
token
)
Gitlab
::
CryptoHelper
.
aes256_gcm_decrypt
(
token
)
end
end
end
end
def
self
.
encrypt_token
(
plaintext_token
)
return
Gitlab
::
CryptoHelper
.
aes256_gcm_encrypt
(
plaintext_token
)
unless
Feature
.
enabled?
(
:dynamic_nonce
,
type: :ops
)
iv
=
::
Digest
::
SHA256
.
hexdigest
(
plaintext_token
).
bytes
.
take
(
NONCE_SIZE
).
pack
(
'c*'
)
token
=
Gitlab
::
CryptoHelper
.
aes256_gcm_encrypt
(
plaintext_token
,
nonce:
iv
)
"
#{
DYNAMIC_NONCE_IDENTIFIER
}#{
token
}#{
iv
}
"
end
end
end
end
end
config/feature_flags/ops/dynamic_nonce.yml
0 → 100644
View file @
1914a1e3
---
name
:
dynamic_nonce
introduced_by_url
:
rollout_issue_url
:
milestone
:
'
14.0'
type
:
ops
group
:
group::access
default_enabled
:
false
spec/models/concerns/token_authenticatable_strategies/encryption_helper_spec.rb
View file @
1914a1e3
...
@@ -3,25 +3,99 @@
...
@@ -3,25 +3,99 @@
require
'spec_helper'
require
'spec_helper'
RSpec
.
describe
TokenAuthenticatableStrategies
::
EncryptionHelper
do
RSpec
.
describe
TokenAuthenticatableStrategies
::
EncryptionHelper
do
let
(
:encrypted_token
)
{
described_class
.
encrypt_token
(
'my-value'
)
}
let
(
:encrypted_token
)
{
described_class
.
encrypt_token
(
'my-value
-my-value-my-value
'
)
}
describe
'.encrypt_token'
do
describe
'.encrypt_token'
do
it
'encrypts token'
do
context
'when dynamic_nonce feature flag is switched on'
do
expect
(
encrypted_token
).
not_to
eq
(
'my-value'
)
it
'adds nonce identifier on the beginning'
do
expect
(
encrypted_token
.
first
).
to
eq
(
described_class
::
DYNAMIC_NONCE_IDENTIFIER
)
end
it
'adds nonce at the end'
do
nonce
=
encrypted_token
.
last
(
described_class
::
NONCE_SIZE
)
expect
(
nonce
).
to
eq
(
::
Digest
::
SHA256
.
hexdigest
(
'my-value-my-value-my-value'
).
bytes
.
take
(
described_class
::
NONCE_SIZE
).
pack
(
'c*'
))
end
it
'encrypts token'
do
expect
(
encrypted_token
[
1
...-
described_class
::
NONCE_SIZE
]).
not_to
eq
(
'my-value-my-value-my-value'
)
end
end
context
'when dynamic_nonce feature flag is switched off'
do
before
do
stub_feature_flags
(
dynamic_nonce:
false
)
end
it
'does not add nonce identifier on the beginning'
do
expect
(
encrypted_token
.
first
).
not_to
eq
(
described_class
::
DYNAMIC_NONCE_IDENTIFIER
)
end
it
'does not add nonce in the end'
do
nonce
=
encrypted_token
.
last
(
described_class
::
NONCE_SIZE
)
expect
(
nonce
).
not_to
eq
(
::
Digest
::
SHA256
.
hexdigest
(
'my-value-my-value-my-value'
).
bytes
.
take
(
described_class
::
NONCE_SIZE
).
pack
(
'c*'
))
end
it
'encrypts token with static iv'
do
token
=
Gitlab
::
CryptoHelper
.
aes256_gcm_encrypt
(
'my-value-my-value-my-value'
)
expect
(
encrypted_token
).
to
eq
(
token
)
end
end
end
end
end
describe
'.decrypt_token'
do
describe
'.decrypt_token'
do
it
'decrypts token with static iv'
do
context
'with feature flag switched off'
do
expect
(
described_class
.
decrypt_token
(
encrypted_token
)).
to
eq
(
'my-value'
)
before
do
stub_feature_flags
(
dynamic_nonce:
false
)
end
it
'decrypts token with static iv'
do
encrypted_token
=
described_class
.
encrypt_token
(
'my-value'
)
expect
(
described_class
.
decrypt_token
(
encrypted_token
)).
to
eq
(
'my-value'
)
end
it
'decrypts token if feature flag changed after encryption'
do
encrypted_token
=
described_class
.
encrypt_token
(
'my-value'
)
expect
(
encrypted_token
).
not_to
eq
(
'my-value'
)
stub_feature_flags
(
dynamic_nonce:
true
)
expect
(
described_class
.
decrypt_token
(
encrypted_token
)).
to
eq
(
'my-value'
)
end
it
'decrypts token with dynamic iv'
do
iv
=
::
Digest
::
SHA256
.
hexdigest
(
'my-value'
).
bytes
.
take
(
described_class
::
NONCE_SIZE
).
pack
(
'c*'
)
token
=
Gitlab
::
CryptoHelper
.
aes256_gcm_encrypt
(
'my-value'
,
nonce:
iv
)
encrypted_token
=
"
#{
described_class
::
DYNAMIC_NONCE_IDENTIFIER
}#{
token
}#{
iv
}
"
expect
(
described_class
.
decrypt_token
(
encrypted_token
)).
to
eq
(
'my-value'
)
end
end
end
it
'decrypts token with dynamic iv'
do
context
'with feature flag switched on'
do
iv
=
::
Digest
::
SHA256
.
hexdigest
(
'my-value'
).
bytes
.
take
(
described_class
::
NONCE_SIZE
).
pack
(
'c*'
)
before
do
token
=
Gitlab
::
CryptoHelper
.
aes256_gcm_encrypt
(
'my-value'
,
nonce:
iv
)
stub_feature_flags
(
dynamic_nonce:
true
)
encrypted_token
=
"
#{
described_class
::
DYNAMIC_NONCE_IDENTIFIER
}#{
token
}#{
iv
}
"
end
it
'decrypts token with dynamic iv'
do
encrypted_token
=
described_class
.
encrypt_token
(
'my-value'
)
expect
(
described_class
.
decrypt_token
(
encrypted_token
)).
to
eq
(
'my-value'
)
end
it
'decrypts token if feature flag changed after encryption'
do
encrypted_token
=
described_class
.
encrypt_token
(
'my-value'
)
expect
(
encrypted_token
).
not_to
eq
(
'my-value'
)
stub_feature_flags
(
dynamic_nonce:
false
)
expect
(
described_class
.
decrypt_token
(
encrypted_token
)).
to
eq
(
'my-value'
)
expect
(
described_class
.
decrypt_token
(
encrypted_token
)).
to
eq
(
'my-value'
)
end
end
end
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment