Fix vault-k8s expected response type

Vault server response for lease_duration is an integer, rather than a string
This commit is contained in:
Matt Leung 2018-05-02 16:54:34 -07:00
parent 4063d29b44
commit 75fbaf91d9
2 changed files with 6 additions and 9 deletions

View file

@ -12,13 +12,13 @@ Vault JSON Response
{ {
"auth": { "auth": {
"client_token" = "token", "client_token" = "token",
"lease_duration" = "1234s" "lease_duration" = 1234
} }
} }
*/ */
type vaultAuth struct { type vaultAuth struct {
Token string `json:"client_token"` Token string `json:"client_token"`
Lease string `json:"lease_duration"` Lease int `json:"lease_duration"`
} }
type vaultResp struct { type vaultResp struct {
Auth vaultAuth Auth vaultAuth
@ -42,10 +42,7 @@ func getKubernetesToken(addr, role, mount, tokenFile string) (string, time.Durat
return "", 0, err return "", 0, err
} }
ttl, err := time.ParseDuration(resp.Auth.Lease) ttl := time.Duration(resp.Auth.Lease) * time.Second
if err != nil {
return "", 0, err
}
return resp.Auth.Token, ttl, nil return resp.Auth.Token, ttl, nil
} }

View file

@ -17,9 +17,9 @@ func TestGetKubernetesToken(t *testing.T) {
b, _ := ioutil.ReadFile(fakeJwtFile) b, _ := ioutil.ReadFile(fakeJwtFile)
fakeJwt := string(b) fakeJwt := string(b)
fakeClientToken := "fakeClientToken" fakeClientToken := "fakeClientToken"
fakeLeaseMinutes := "10m" fakeLeaseSeconds := 86400
fakeLeaseDuration, _ := time.ParseDuration(fakeLeaseMinutes) fakeLeaseDuration := time.Duration(fakeLeaseSeconds) * time.Second
fakeResp := fmt.Sprintf("{\"auth\": {\"client_token\": \"%s\", \"lease_duration\": \"%s\"}}", fakeClientToken, fakeLeaseMinutes) fakeResp := fmt.Sprintf("{\"auth\": {\"client_token\": \"%s\", \"lease_duration\": %d}}", fakeClientToken, fakeLeaseSeconds)
expectedPath := "/v1/auth/kubernetes/login" expectedPath := "/v1/auth/kubernetes/login"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {