Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Vault
Commits
7d520d40
Unverified
Commit
7d520d40
authored
3 years ago
by
Anton Averchenkov
Committed by
GitHub
3 years ago
Browse files
Options
Download
Email Patches
Plain Diff
Use WriteWithContext in auth helpers (#14775)
parent
16a23cc3
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
api/auth/approle/approle.go
+6
-2
api/auth/approle/approle.go
api/auth/aws/aws.go
+5
-1
api/auth/aws/aws.go
api/auth/azure/azure.go
+5
-1
api/auth/azure/azure.go
api/auth/gcp/gcp.go
+5
-1
api/auth/gcp/gcp.go
api/auth/kubernetes/kubernetes.go
+5
-1
api/auth/kubernetes/kubernetes.go
api/auth/ldap/ldap.go
+5
-1
api/auth/ldap/ldap.go
api/auth/userpass/userpass.go
+5
-1
api/auth/userpass/userpass.go
changelog/14775.txt
+3
-0
changelog/14775.txt
command/agent/auth/approle/approle.go
+2
-2
command/agent/auth/approle/approle.go
command/agent/auth/auth.go
+2
-2
command/agent/auth/auth.go
with
43 additions
and
12 deletions
+43
-12
api/auth/approle/approle.go
+
6
-
2
View file @
7d520d40
...
...
@@ -100,6 +100,10 @@ func NewAppRoleAuth(roleID string, secretID *SecretID, opts ...LoginOption) (*Ap
}
func
(
a
*
AppRoleAuth
)
Login
(
ctx
context
.
Context
,
client
*
api
.
Client
)
(
*
api
.
Secret
,
error
)
{
if
ctx
==
nil
{
ctx
=
context
.
Background
()
}
loginData
:=
map
[
string
]
interface
{}{
"role_id"
:
a
.
roleID
,
}
...
...
@@ -125,7 +129,7 @@ func (a *AppRoleAuth) Login(ctx context.Context, client *api.Client) (*api.Secre
// if the caller indicated that the value was actually a wrapping token, unwrap it first
if
a
.
unwrap
{
unwrappedToken
,
err
:=
client
.
Logical
()
.
Unwrap
(
secretIDValue
)
unwrappedToken
,
err
:=
client
.
Logical
()
.
Unwrap
WithContext
(
ctx
,
secretIDValue
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"unable to unwrap response wrapping token: %w"
,
err
)
}
...
...
@@ -135,7 +139,7 @@ func (a *AppRoleAuth) Login(ctx context.Context, client *api.Client) (*api.Secre
}
path
:=
fmt
.
Sprintf
(
"auth/%s/login"
,
a
.
mountPath
)
resp
,
err
:=
client
.
Logical
()
.
Write
(
path
,
loginData
)
resp
,
err
:=
client
.
Logical
()
.
Write
WithContext
(
ctx
,
path
,
loginData
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"unable to log in with app role auth: %w"
,
err
)
}
...
...
This diff is collapsed.
Click to expand it.
api/auth/aws/aws.go
+
5
-
1
View file @
7d520d40
...
...
@@ -84,6 +84,10 @@ func NewAWSAuth(opts ...LoginOption) (*AWSAuth, error) {
// variables. To specify a path to a credentials file on disk instead, set
// the environment variable AWS_SHARED_CREDENTIALS_FILE.
func
(
a
*
AWSAuth
)
Login
(
ctx
context
.
Context
,
client
*
api
.
Client
)
(
*
api
.
Secret
,
error
)
{
if
ctx
==
nil
{
ctx
=
context
.
Background
()
}
loginData
:=
make
(
map
[
string
]
interface
{})
switch
a
.
authType
{
case
ec2Type
:
...
...
@@ -182,7 +186,7 @@ func (a *AWSAuth) Login(ctx context.Context, client *api.Client) (*api.Secret, e
}
path
:=
fmt
.
Sprintf
(
"auth/%s/login"
,
a
.
mountPath
)
resp
,
err
:=
client
.
Logical
()
.
Write
(
path
,
loginData
)
resp
,
err
:=
client
.
Logical
()
.
Write
WithContext
(
ctx
,
path
,
loginData
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"unable to log in with AWS auth: %w"
,
err
)
}
...
...
This diff is collapsed.
Click to expand it.
api/auth/azure/azure.go
+
5
-
1
View file @
7d520d40
...
...
@@ -90,6 +90,10 @@ func NewAzureAuth(roleName string, opts ...LoginOption) (*AzureAuth, error) {
// Login sets up the required request body for the Azure auth method's /login
// endpoint, and performs a write to it.
func
(
a
*
AzureAuth
)
Login
(
ctx
context
.
Context
,
client
*
api
.
Client
)
(
*
api
.
Secret
,
error
)
{
if
ctx
==
nil
{
ctx
=
context
.
Background
()
}
jwtResp
,
err
:=
a
.
getJWT
()
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"unable to get access token: %w"
,
err
)
...
...
@@ -110,7 +114,7 @@ func (a *AzureAuth) Login(ctx context.Context, client *api.Client) (*api.Secret,
}
path
:=
fmt
.
Sprintf
(
"auth/%s/login"
,
a
.
mountPath
)
resp
,
err
:=
client
.
Logical
()
.
Write
(
path
,
loginData
)
resp
,
err
:=
client
.
Logical
()
.
Write
WithContext
(
ctx
,
path
,
loginData
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"unable to log in with Azure auth: %w"
,
err
)
}
...
...
This diff is collapsed.
Click to expand it.
api/auth/gcp/gcp.go
+
5
-
1
View file @
7d520d40
...
...
@@ -67,6 +67,10 @@ func NewGCPAuth(roleName string, opts ...LoginOption) (*GCPAuth, error) {
// endpoint, and performs a write to it. This method defaults to the "gce"
// auth type unless NewGCPAuth is called with WithIAMAuth().
func
(
a
*
GCPAuth
)
Login
(
ctx
context
.
Context
,
client
*
api
.
Client
)
(
*
api
.
Secret
,
error
)
{
if
ctx
==
nil
{
ctx
=
context
.
Background
()
}
loginData
:=
map
[
string
]
interface
{}{
"role"
:
a
.
roleName
,
}
...
...
@@ -86,7 +90,7 @@ func (a *GCPAuth) Login(ctx context.Context, client *api.Client) (*api.Secret, e
}
path
:=
fmt
.
Sprintf
(
"auth/%s/login"
,
a
.
mountPath
)
resp
,
err
:=
client
.
Logical
()
.
Write
(
path
,
loginData
)
resp
,
err
:=
client
.
Logical
()
.
Write
WithContext
(
ctx
,
path
,
loginData
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"unable to log in with GCP auth: %w"
,
err
)
}
...
...
This diff is collapsed.
Click to expand it.
api/auth/kubernetes/kubernetes.go
+
5
-
1
View file @
7d520d40
...
...
@@ -68,13 +68,17 @@ func NewKubernetesAuth(roleName string, opts ...LoginOption) (*KubernetesAuth, e
}
func
(
a
*
KubernetesAuth
)
Login
(
ctx
context
.
Context
,
client
*
api
.
Client
)
(
*
api
.
Secret
,
error
)
{
if
ctx
==
nil
{
ctx
=
context
.
Background
()
}
loginData
:=
map
[
string
]
interface
{}{
"jwt"
:
a
.
serviceAccountToken
,
"role"
:
a
.
roleName
,
}
path
:=
fmt
.
Sprintf
(
"auth/%s/login"
,
a
.
mountPath
)
resp
,
err
:=
client
.
Logical
()
.
Write
(
path
,
loginData
)
resp
,
err
:=
client
.
Logical
()
.
Write
WithContext
(
ctx
,
path
,
loginData
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"unable to log in with Kubernetes auth: %w"
,
err
)
}
...
...
This diff is collapsed.
Click to expand it.
api/auth/ldap/ldap.go
+
5
-
1
View file @
7d520d40
...
...
@@ -84,6 +84,10 @@ func NewLDAPAuth(username string, password *Password, opts ...LoginOption) (*LDA
}
func
(
a
*
LDAPAuth
)
Login
(
ctx
context
.
Context
,
client
*
api
.
Client
)
(
*
api
.
Secret
,
error
)
{
if
ctx
==
nil
{
ctx
=
context
.
Background
()
}
loginData
:=
make
(
map
[
string
]
interface
{})
if
a
.
passwordFile
!=
""
{
...
...
@@ -103,7 +107,7 @@ func (a *LDAPAuth) Login(ctx context.Context, client *api.Client) (*api.Secret,
}
path
:=
fmt
.
Sprintf
(
"auth/%s/login/%s"
,
a
.
mountPath
,
a
.
username
)
resp
,
err
:=
client
.
Logical
()
.
Write
(
path
,
loginData
)
resp
,
err
:=
client
.
Logical
()
.
Write
WithContext
(
ctx
,
path
,
loginData
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"unable to log in with LDAP auth: %w"
,
err
)
}
...
...
This diff is collapsed.
Click to expand it.
api/auth/userpass/userpass.go
+
5
-
1
View file @
7d520d40
...
...
@@ -88,6 +88,10 @@ func NewUserpassAuth(username string, password *Password, opts ...LoginOption) (
}
func
(
a
*
UserpassAuth
)
Login
(
ctx
context
.
Context
,
client
*
api
.
Client
)
(
*
api
.
Secret
,
error
)
{
if
ctx
==
nil
{
ctx
=
context
.
Background
()
}
loginData
:=
make
(
map
[
string
]
interface
{})
if
a
.
passwordFile
!=
""
{
...
...
@@ -107,7 +111,7 @@ func (a *UserpassAuth) Login(ctx context.Context, client *api.Client) (*api.Secr
}
path
:=
fmt
.
Sprintf
(
"auth/%s/login/%s"
,
a
.
mountPath
,
a
.
username
)
resp
,
err
:=
client
.
Logical
()
.
Write
(
path
,
loginData
)
resp
,
err
:=
client
.
Logical
()
.
Write
WithContext
(
ctx
,
path
,
loginData
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"unable to log in with userpass auth: %w"
,
err
)
}
...
...
This diff is collapsed.
Click to expand it.
changelog/14775.txt
0 → 100644
+
3
-
0
View file @
7d520d40
```release-note:improvement
api: Use the context passed to the api/auth Login helpers.
```
This diff is collapsed.
Click to expand it.
command/agent/auth/approle/approle.go
+
2
-
2
View file @
7d520d40
...
...
@@ -138,7 +138,7 @@ func (a *approleMethod) Authenticate(ctx context.Context, client *api.Client) (s
}
clonedClient
.
SetToken
(
stringSecretID
)
// Validate the creation path
resp
,
err
:=
clonedClient
.
Logical
()
.
Read
(
"sys/wrapping/lookup"
)
resp
,
err
:=
clonedClient
.
Logical
()
.
Read
WithContext
(
ctx
,
"sys/wrapping/lookup"
)
if
err
!=
nil
{
return
""
,
nil
,
nil
,
fmt
.
Errorf
(
"error looking up wrapped secret ID: %w"
,
err
)
}
...
...
@@ -161,7 +161,7 @@ func (a *approleMethod) Authenticate(ctx context.Context, client *api.Client) (s
return
""
,
nil
,
nil
,
errors
.
New
(
"unable to validate wrapping token creation path"
)
}
// Now get the secret ID
resp
,
err
=
clonedClient
.
Logical
()
.
Unwrap
(
""
)
resp
,
err
=
clonedClient
.
Logical
()
.
Unwrap
WithContext
(
ctx
,
""
)
if
err
!=
nil
{
return
""
,
nil
,
nil
,
fmt
.
Errorf
(
"error unwrapping secret ID: %w"
,
err
)
}
...
...
This diff is collapsed.
Click to expand it.
command/agent/auth/auth.go
+
2
-
2
View file @
7d520d40
...
...
@@ -172,7 +172,7 @@ func (ah *AuthHandler) Run(ctx context.Context, am AuthMethod) error {
ah
.
logger
.
Debug
(
"lookup-self with preloaded token"
)
clientToUse
.
SetToken
(
ah
.
token
)
secret
,
err
=
clientToUse
.
Logical
()
.
Read
(
"auth/token/l
ookup
-s
elf
"
)
secret
,
err
=
clientToUse
.
Auth
()
.
Token
()
.
L
ookup
S
elf
WithContext
(
ctx
)
if
err
!=
nil
{
ah
.
logger
.
Error
(
"could not look up token"
,
"err"
,
err
,
"backoff"
,
backoff
)
backoffOrQuit
(
ctx
,
backoff
)
...
...
@@ -220,7 +220,7 @@ func (ah *AuthHandler) Run(ctx context.Context, am AuthMethod) error {
// This should only happen if there's no preloaded token (regular auto-auth login)
// or if a preloaded token has expired and is now switching to auto-auth.
if
secret
.
Auth
==
nil
{
secret
,
err
=
clientToUse
.
Logical
()
.
Write
(
path
,
data
)
secret
,
err
=
clientToUse
.
Logical
()
.
Write
WithContext
(
ctx
,
path
,
data
)
// Check errors/sanity
if
err
!=
nil
{
ah
.
logger
.
Error
(
"error authenticating"
,
"error"
,
err
,
"backoff"
,
backoff
)
...
...
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
Menu
Projects
Groups
Snippets
Help