2020-07-27 23:49:43 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 VMware, Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
package client
|
|
|
|
|
2020-07-28 14:10:40 +00:00
|
|
|
import (
|
2020-07-29 21:24:16 +00:00
|
|
|
"bytes"
|
2020-07-28 14:10:40 +00:00
|
|
|
"context"
|
2020-07-29 21:24:16 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/json"
|
2020-07-28 15:27:07 +00:00
|
|
|
"fmt"
|
2020-07-29 21:24:16 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-08-14 14:11:14 +00:00
|
|
|
// ErrCredentialRequestFailed is returned by ExchangeToken when the server rejects the credential request.
|
|
|
|
ErrCredentialRequestFailed = fmt.Errorf("credential request failed")
|
2020-07-27 23:49:43 +00:00
|
|
|
|
2020-07-29 21:24:16 +00:00
|
|
|
// ErrInvalidAPIEndpoint is returned by ExchangeToken when the provided API endpoint is invalid.
|
|
|
|
ErrInvalidAPIEndpoint = fmt.Errorf("invalid API endpoint")
|
2020-07-28 15:27:07 +00:00
|
|
|
|
2020-07-29 21:24:16 +00:00
|
|
|
// ErrInvalidCABundle is returned by ExchangeToken when the provided CA bundle is invalid.
|
|
|
|
ErrInvalidCABundle = fmt.Errorf("invalid CA bundle")
|
2020-07-28 14:10:40 +00:00
|
|
|
)
|
|
|
|
|
2020-07-29 21:24:16 +00:00
|
|
|
const (
|
2020-08-14 14:11:14 +00:00
|
|
|
// credentialRequestsAPIPath is the API path for the v1alpha1 CredentialRequest API.
|
2020-08-20 17:54:15 +00:00
|
|
|
credentialRequestsAPIPath = "/apis/pinniped.dev/v1alpha1/credentialrequests"
|
2020-07-28 15:27:07 +00:00
|
|
|
|
2020-07-29 21:24:16 +00:00
|
|
|
// userAgent is the user agent header value sent with requests.
|
2020-08-20 17:54:15 +00:00
|
|
|
userAgent = "pinniped"
|
2020-07-29 21:24:16 +00:00
|
|
|
)
|
|
|
|
|
2020-08-14 14:11:14 +00:00
|
|
|
func credentialRequest(ctx context.Context, apiEndpoint *url.URL, token string) (*http.Request, error) {
|
|
|
|
type CredentialRequestTokenCredential struct {
|
2020-07-29 21:24:16 +00:00
|
|
|
Value string `json:"value"`
|
|
|
|
}
|
2020-08-14 14:11:14 +00:00
|
|
|
type CredentialRequestSpec struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Token *CredentialRequestTokenCredential `json:"token"`
|
2020-07-29 21:24:16 +00:00
|
|
|
}
|
|
|
|
body := struct {
|
|
|
|
APIVersion string `json:"apiVersion"`
|
|
|
|
Kind string `json:"kind"`
|
|
|
|
Metadata struct {
|
|
|
|
CreationTimestamp *string `json:"creationTimestamp"`
|
|
|
|
} `json:"metadata"`
|
2020-08-14 14:11:14 +00:00
|
|
|
Spec CredentialRequestSpec `json:"spec"`
|
|
|
|
Status struct{} `json:"status"`
|
2020-07-29 21:24:16 +00:00
|
|
|
}{
|
2020-08-20 17:54:15 +00:00
|
|
|
APIVersion: "pinniped.dev/v1alpha1",
|
2020-08-14 14:11:14 +00:00
|
|
|
Kind: "CredentialRequest",
|
|
|
|
Spec: CredentialRequestSpec{Type: "token", Token: &CredentialRequestTokenCredential{Value: token}},
|
2020-07-29 21:24:16 +00:00
|
|
|
}
|
|
|
|
bodyJSON, err := json.Marshal(&body)
|
2020-07-28 15:27:07 +00:00
|
|
|
if err != nil {
|
2020-07-29 21:24:16 +00:00
|
|
|
return nil, err
|
2020-07-28 15:27:07 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 21:24:16 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, apiEndpoint.String(), bytes.NewReader(bodyJSON))
|
2020-07-28 15:27:07 +00:00
|
|
|
if err != nil {
|
2020-07-29 21:24:16 +00:00
|
|
|
return nil, err
|
2020-07-28 15:27:07 +00:00
|
|
|
}
|
2020-07-29 21:24:16 +00:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req.Header.Set("User-Agent", userAgent)
|
|
|
|
return req, nil
|
|
|
|
}
|
2020-07-28 15:27:07 +00:00
|
|
|
|
2020-07-29 21:24:16 +00:00
|
|
|
// Credential is the output of an ExchangeToken operation. It is equivalent to the data
|
|
|
|
// in the Kubernetes client.authentication.k8s.io/v1beta1 ExecCredentialStatus type.
|
|
|
|
type Credential struct {
|
|
|
|
// ExpirationTimestamp indicates a time when the provided credentials expire.
|
|
|
|
ExpirationTimestamp *time.Time
|
|
|
|
|
|
|
|
// Token is a bearer token used by the client for request authentication.
|
|
|
|
Token string
|
|
|
|
|
|
|
|
// PEM-encoded client TLS certificates (including intermediates, if any).
|
|
|
|
ClientCertificateData string
|
|
|
|
|
|
|
|
// PEM-encoded private key for the above certificate.
|
|
|
|
ClientKeyData string
|
2020-07-28 15:27:07 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 21:24:16 +00:00
|
|
|
func ExchangeToken(ctx context.Context, token, caBundle, apiEndpoint string) (*Credential, error) {
|
|
|
|
// Parse and validate the provided API endpoint.
|
|
|
|
endpointURL, err := url.Parse(apiEndpoint)
|
2020-07-28 15:27:07 +00:00
|
|
|
if err != nil {
|
2020-07-29 21:24:16 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s", ErrInvalidAPIEndpoint, err.Error())
|
2020-07-28 15:27:07 +00:00
|
|
|
}
|
2020-07-29 21:24:16 +00:00
|
|
|
if endpointURL.Scheme != "https" {
|
|
|
|
return nil, fmt.Errorf(`%w: protocol must be "https", not %q`, ErrInvalidAPIEndpoint, endpointURL.Scheme)
|
|
|
|
}
|
|
|
|
|
2020-08-14 14:11:14 +00:00
|
|
|
// Form the CredentialRequest API URL by appending the API path to the main API endpoint.
|
2020-08-20 17:54:15 +00:00
|
|
|
pinnipedEndpointURL := *endpointURL
|
|
|
|
pinnipedEndpointURL.Path = filepath.Join(pinnipedEndpointURL.Path, credentialRequestsAPIPath)
|
2020-07-29 21:24:16 +00:00
|
|
|
|
|
|
|
// Initialize a TLS client configuration from the provided CA bundle.
|
|
|
|
tlsConfig := tls.Config{
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
RootCAs: x509.NewCertPool(),
|
|
|
|
}
|
|
|
|
if !tlsConfig.RootCAs.AppendCertsFromPEM([]byte(caBundle)) {
|
|
|
|
return nil, fmt.Errorf("%w: no certificates found", ErrInvalidCABundle)
|
|
|
|
}
|
|
|
|
|
2020-08-20 17:54:15 +00:00
|
|
|
// Create a request object for the "POST /apis/pinniped.dev/v1alpha1/credentialrequests" request.
|
|
|
|
req, err := credentialRequest(ctx, &pinnipedEndpointURL, token)
|
2020-07-29 21:24:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not build request: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client := http.Client{Transport: &http.Transport{TLSClientConfig: &tlsConfig}}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2020-08-14 14:11:14 +00:00
|
|
|
return nil, fmt.Errorf("could not get credential: %w", err)
|
2020-07-29 21:24:16 +00:00
|
|
|
}
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
if resp.StatusCode != http.StatusCreated {
|
2020-08-14 14:11:14 +00:00
|
|
|
return nil, fmt.Errorf("%w: server returned status %d", ErrCredentialRequestFailed, resp.StatusCode)
|
2020-07-29 21:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var respBody struct {
|
|
|
|
APIVersion string `json:"apiVersion"`
|
|
|
|
Kind string `json:"kind"`
|
|
|
|
Status struct {
|
|
|
|
Credential *struct {
|
2020-07-30 21:25:59 +00:00
|
|
|
ExpirationTimestamp string `json:"expirationTimestamp"`
|
|
|
|
Token string `json:"token"`
|
2020-07-29 21:24:16 +00:00
|
|
|
ClientCertificateData string `json:"clientCertificateData"`
|
|
|
|
ClientKeyData string `json:"clientKeyData"`
|
|
|
|
}
|
|
|
|
Message string `json:"message"`
|
|
|
|
} `json:"status"`
|
|
|
|
}
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&respBody); err != nil {
|
2020-08-14 14:11:14 +00:00
|
|
|
return nil, fmt.Errorf("invalid credential response: %w", err)
|
2020-07-29 21:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if respBody.Status.Credential == nil || respBody.Status.Message != "" {
|
2020-08-14 14:11:14 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s", ErrCredentialRequestFailed, respBody.Status.Message)
|
2020-07-29 21:24:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 21:25:59 +00:00
|
|
|
result := Credential{
|
|
|
|
Token: respBody.Status.Credential.Token,
|
2020-07-29 21:24:16 +00:00
|
|
|
ClientCertificateData: respBody.Status.Credential.ClientCertificateData,
|
|
|
|
ClientKeyData: respBody.Status.Credential.ClientKeyData,
|
2020-07-30 21:25:59 +00:00
|
|
|
}
|
|
|
|
if str := respBody.Status.Credential.ExpirationTimestamp; str != "" {
|
|
|
|
expiration, err := time.Parse(time.RFC3339, str)
|
|
|
|
if err != nil {
|
2020-08-14 14:11:14 +00:00
|
|
|
return nil, fmt.Errorf("invalid credential response: %w", err)
|
2020-07-30 21:25:59 +00:00
|
|
|
}
|
|
|
|
result.ExpirationTimestamp = &expiration
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result, nil
|
2020-07-27 23:49:43 +00:00
|
|
|
}
|