Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
ad17292f
Commit
ad17292f
authored
Jul 21, 2021
by
galal-hussein
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
migrate empty string key properly
Signed-off-by:
galal-hussein
<
hussein.galal.ahmed.11@gmail.com
>
parent
a65e5b64
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
57 additions
and
19 deletions
+57
-19
storage.go
pkg/cluster/storage.go
+57
-19
No files found.
pkg/cluster/storage.go
View file @
ad17292f
...
@@ -47,7 +47,7 @@ func (c *Cluster) save(ctx context.Context) error {
...
@@ -47,7 +47,7 @@ func (c *Cluster) save(ctx context.Context) error {
return
err
return
err
}
}
_
,
_
,
err
=
c
.
getBootstrapKeyFromStorage
(
ctx
,
storageClient
,
normalizedToken
)
_
,
err
=
c
.
getBootstrapKeyFromStorage
(
ctx
,
storageClient
,
normalizedToken
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
...
@@ -97,16 +97,14 @@ func (c *Cluster) storageBootstrap(ctx context.Context) error {
...
@@ -97,16 +97,14 @@ func (c *Cluster) storageBootstrap(ctx context.Context) error {
return
err
return
err
}
}
value
,
e
mptyKey
,
e
rr
:=
c
.
getBootstrapKeyFromStorage
(
ctx
,
storageClient
,
normalizedToken
)
value
,
err
:=
c
.
getBootstrapKeyFromStorage
(
ctx
,
storageClient
,
normalizedToken
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
if
value
==
nil
{
if
value
==
nil
{
return
nil
return
nil
}
}
if
emptyKey
{
normalizedToken
=
""
}
data
,
err
:=
decrypt
(
normalizedToken
,
value
.
Data
)
data
,
err
:=
decrypt
(
normalizedToken
,
value
.
Data
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
...
@@ -119,37 +117,39 @@ func (c *Cluster) storageBootstrap(ctx context.Context) error {
...
@@ -119,37 +117,39 @@ func (c *Cluster) storageBootstrap(ctx context.Context) error {
// hashed with empty string and will check for any key that is hashed by different token than the one
// hashed with empty string and will check for any key that is hashed by different token than the one
// passed to it, it will return error if it finds a key that is hashed with different token and will return
// passed to it, it will return error if it finds a key that is hashed with different token and will return
// value if it finds the key hashed by passed token or empty string
// value if it finds the key hashed by passed token or empty string
func
(
c
*
Cluster
)
getBootstrapKeyFromStorage
(
ctx
context
.
Context
,
storageClient
client
.
Client
,
token
string
)
(
*
client
.
Value
,
bool
,
error
)
{
func
(
c
*
Cluster
)
getBootstrapKeyFromStorage
(
ctx
context
.
Context
,
storageClient
client
.
Client
,
normalizedToken
string
)
(
*
client
.
Value
,
error
)
{
emptyStringKey
:=
storageKey
(
""
)
emptyStringKey
:=
storageKey
(
""
)
tokenKey
:=
storageKey
(
token
)
tokenKey
:=
storageKey
(
normalizedToken
)
bootstrapList
,
err
:=
storageClient
.
List
(
ctx
,
"/bootstrap"
,
0
)
bootstrapList
,
err
:=
storageClient
.
List
(
ctx
,
"/bootstrap"
,
0
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
false
,
err
return
nil
,
err
}
}
if
len
(
bootstrapList
)
==
0
{
if
len
(
bootstrapList
)
==
0
{
c
.
saveBootstrap
=
true
c
.
saveBootstrap
=
true
return
nil
,
false
,
nil
return
nil
,
nil
}
}
if
len
(
bootstrapList
)
>
1
{
if
len
(
bootstrapList
)
>
1
{
logrus
.
Warn
(
"found multiple bootstrap keys in storage"
)
logrus
.
Warn
(
"found multiple bootstrap keys in storage"
)
}
}
// check for empty string key
if
err
:=
c
.
migrateEmptyStringToken
(
ctx
,
bootstrapList
,
storageClient
,
emptyStringKey
,
tokenKey
,
normalizedToken
);
err
!=
nil
{
return
nil
,
err
}
// getting the list of bootstrap again after migrating the empty key
bootstrapList
,
err
=
storageClient
.
List
(
ctx
,
"/bootstrap"
,
0
)
if
err
!=
nil
{
return
nil
,
err
}
for
_
,
bootstrapKV
:=
range
bootstrapList
{
for
_
,
bootstrapKV
:=
range
bootstrapList
{
// checking for empty string bootstrap key
// checking for empty string bootstrap key
switch
string
(
bootstrapKV
.
Key
)
{
switch
string
(
bootstrapKV
.
Key
)
{
case
emptyStringKey
:
logrus
.
Warn
(
"bootstrap data encrypted with empty string, deleting and resaving with token"
)
c
.
saveBootstrap
=
true
if
err
:=
storageClient
.
Delete
(
ctx
,
emptyStringKey
,
bootstrapKV
.
Modified
);
err
!=
nil
{
return
nil
,
false
,
err
}
return
&
bootstrapKV
,
true
,
nil
case
tokenKey
:
case
tokenKey
:
return
&
bootstrapKV
,
false
,
nil
return
&
bootstrapKV
,
nil
}
}
}
}
return
nil
,
false
,
errors
.
New
(
"bootstrap data already found and encrypted with different token"
)
return
nil
,
errors
.
New
(
"bootstrap data already found and encrypted with different token"
)
}
}
// readTokenFromFile will attempt to get the token from <data-dir>/token if it the file not found
// readTokenFromFile will attempt to get the token from <data-dir>/token if it the file not found
...
@@ -181,3 +181,41 @@ func normalizeToken(token string) (string, error) {
...
@@ -181,3 +181,41 @@ func normalizeToken(token string) (string, error) {
}
}
return
password
,
nil
return
password
,
nil
}
}
// migrateEmptyStringToken will list all keys that has prefix /bootstrap and will check for key that is
// hashed with empty string and then migrate this empty string key and resave it with the right token
func
(
c
*
Cluster
)
migrateEmptyStringToken
(
ctx
context
.
Context
,
bootstrapList
[]
client
.
Value
,
storageClient
client
.
Client
,
emptyStringKey
,
tokenKey
,
token
string
)
error
{
for
_
,
bootstrapKV
:=
range
bootstrapList
{
// checking for empty string bootstrap key
if
string
(
bootstrapKV
.
Key
)
==
emptyStringKey
{
logrus
.
Warn
(
"bootstrap data encrypted with empty string, deleting and resaving with token"
)
// making sure that the process is autonmous by decrypting and rencrypting the data first
data
,
err
:=
decrypt
(
""
,
bootstrapKV
.
Data
)
if
err
!=
nil
{
return
err
}
encryptedData
,
err
:=
encrypt
(
token
,
data
)
if
err
!=
nil
{
return
err
}
// saving the new encrypted data with the right token key
if
err
:=
storageClient
.
Create
(
ctx
,
tokenKey
,
encryptedData
);
err
!=
nil
{
if
err
.
Error
()
==
"key exists"
{
logrus
.
Warnln
(
"bootstrap key exists; please follow documentation on updating a node after snapshot restore"
)
return
nil
}
else
if
strings
.
Contains
(
err
.
Error
(),
"not supported for learner"
)
{
logrus
.
Debug
(
"skipping bootstrap data save on learner"
)
return
nil
}
return
err
}
// deleting the empty string key
if
err
:=
storageClient
.
Delete
(
ctx
,
emptyStringKey
,
bootstrapKV
.
Modified
);
err
!=
nil
{
return
err
}
return
nil
}
}
return
nil
}
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