Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Nomad
Commits
043e7f48
Commit
043e7f48
authored
8 years ago
by
Alex Dadgar
Committed by
GitHub
8 years ago
Browse files
Options
Download
Plain Diff
Merge pull request #2503 from hashicorp/debug-vault
Fix variable capture and add tests
parents
a8b836be
031303a6
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
client/task_runner.go
+9
-3
client/task_runner.go
client/task_runner_test.go
+45
-0
client/task_runner_test.go
nomad/node_endpoint.go
+1
-1
nomad/node_endpoint.go
with
55 additions
and
4 deletions
+55
-4
client/task_runner.go
+
9
-
3
View file @
043e7f48
...
...
@@ -553,9 +553,12 @@ func (f *tokenFuture) Get() string {
// allows setting the initial Vault token. This is useful when the Vault token
// is recovered off disk.
func
(
r
*
TaskRunner
)
vaultManager
(
token
string
)
{
// Always stop renewing the token. If token is empty or untracked, it is a
// no-op so this is always safe.
defer
r
.
vaultClient
.
StopRenewToken
(
r
.
vaultFuture
.
Get
())
// Helper for stopping token renewal
stopRenewal
:=
func
()
{
if
err
:=
r
.
vaultClient
.
StopRenewToken
(
r
.
vaultFuture
.
Get
());
err
!=
nil
{
r
.
logger
.
Printf
(
"[WARN] client: failed to stop token renewal for task %v in alloc %q: %v"
,
r
.
task
.
Name
,
r
.
alloc
.
ID
,
err
)
}
}
// updatedToken lets us store state between loops. If true, a new token
// has been retrieved and we need to apply the Vault change mode
...
...
@@ -566,6 +569,7 @@ OUTER:
// Check if we should exit
select
{
case
<-
r
.
waitCh
:
stopRenewal
()
return
default
:
}
...
...
@@ -643,12 +647,14 @@ OUTER:
// Clear the token
token
=
""
r
.
logger
.
Printf
(
"[ERR] client: failed to renew Vault token for task %v on alloc %q: %v"
,
r
.
task
.
Name
,
r
.
alloc
.
ID
,
err
)
stopRenewal
()
// Check if we have to do anything
if
r
.
task
.
Vault
.
ChangeMode
!=
structs
.
VaultChangeModeNoop
{
updatedToken
=
true
}
case
<-
r
.
waitCh
:
stopRenewal
()
return
}
}
...
...
This diff is collapsed.
Click to expand it.
client/task_runner_test.go
+
45
-
0
View file @
043e7f48
...
...
@@ -876,6 +876,21 @@ func TestTaskRunner_BlockForVault(t *testing.T) {
if
act
:=
string
(
data
);
act
!=
token
{
t
.
Fatalf
(
"Token didn't get written to disk properly, got %q; want %q"
,
act
,
token
)
}
// Check the token was revoked
m
:=
ctx
.
tr
.
vaultClient
.
(
*
vaultclient
.
MockVaultClient
)
testutil
.
WaitForResult
(
func
()
(
bool
,
error
)
{
if
len
(
m
.
StoppedTokens
)
!=
1
{
return
false
,
fmt
.
Errorf
(
"Expected a stopped token: %v"
,
m
.
StoppedTokens
)
}
if
a
:=
m
.
StoppedTokens
[
0
];
a
!=
token
{
return
false
,
fmt
.
Errorf
(
"got stopped token %q; want %q"
,
a
,
token
)
}
return
true
,
nil
},
func
(
err
error
)
{
t
.
Fatalf
(
"err: %v"
,
err
)
})
}
func
TestTaskRunner_DeriveToken_Retry
(
t
*
testing
.
T
)
{
...
...
@@ -946,6 +961,21 @@ func TestTaskRunner_DeriveToken_Retry(t *testing.T) {
if
act
:=
string
(
data
);
act
!=
token
{
t
.
Fatalf
(
"Token didn't get written to disk properly, got %q; want %q"
,
act
,
token
)
}
// Check the token was revoked
m
:=
ctx
.
tr
.
vaultClient
.
(
*
vaultclient
.
MockVaultClient
)
testutil
.
WaitForResult
(
func
()
(
bool
,
error
)
{
if
len
(
m
.
StoppedTokens
)
!=
1
{
return
false
,
fmt
.
Errorf
(
"Expected a stopped token: %v"
,
m
.
StoppedTokens
)
}
if
a
:=
m
.
StoppedTokens
[
0
];
a
!=
token
{
return
false
,
fmt
.
Errorf
(
"got stopped token %q; want %q"
,
a
,
token
)
}
return
true
,
nil
},
func
(
err
error
)
{
t
.
Fatalf
(
"err: %v"
,
err
)
})
}
func
TestTaskRunner_DeriveToken_Unrecoverable
(
t
*
testing
.
T
)
{
...
...
@@ -1215,6 +1245,21 @@ func TestTaskRunner_Template_NewVaultToken(t *testing.T) {
},
func
(
err
error
)
{
t
.
Fatalf
(
"err: %v"
,
err
)
})
// Check the token was revoked
m
:=
ctx
.
tr
.
vaultClient
.
(
*
vaultclient
.
MockVaultClient
)
testutil
.
WaitForResult
(
func
()
(
bool
,
error
)
{
if
len
(
m
.
StoppedTokens
)
!=
1
{
return
false
,
fmt
.
Errorf
(
"Expected a stopped token: %v"
,
m
.
StoppedTokens
)
}
if
a
:=
m
.
StoppedTokens
[
0
];
a
!=
token
{
return
false
,
fmt
.
Errorf
(
"got stopped token %q; want %q"
,
a
,
token
)
}
return
true
,
nil
},
func
(
err
error
)
{
t
.
Fatalf
(
"err: %v"
,
err
)
})
}
func
TestTaskRunner_VaultManager_Restart
(
t
*
testing
.
T
)
{
...
...
This diff is collapsed.
Click to expand it.
nomad/node_endpoint.go
+
1
-
1
View file @
043e7f48
...
...
@@ -1123,7 +1123,7 @@ func (n *Node) DeriveVaultToken(args *structs.DeriveVaultTokenRequest,
if
rerr
,
ok
:=
createErr
.
(
*
structs
.
RecoverableError
);
ok
{
reply
.
Error
=
rerr
}
else
if
err
!=
nil
{
}
else
{
reply
.
Error
=
structs
.
NewRecoverableError
(
createErr
,
false
)
.
(
*
structs
.
RecoverableError
)
}
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
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