Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the apiversion from KUBERNETES_EXEC_INFO #439

Merged
merged 5 commits into from Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/aws-iam-authenticator/verify.go
Expand Up @@ -21,9 +21,11 @@ import (
"fmt"
"os"

"sigs.k8s.io/aws-iam-authenticator/pkg/metrics"
"sigs.k8s.io/aws-iam-authenticator/pkg/token"

"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/prometheus/client_golang/prometheus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -70,6 +72,7 @@ var verifyCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(verifyCmd)
metrics.InitMetrics(prometheus.DefaultRegisterer)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixes #438

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this change unrelated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. it fixes #438
i can get rid of it if causing confusion. it was in my workspace, so i let it be part of the PR

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably would be best to isolate this to a different PR, thanks Jyoti. Then you can link to #438 from that other PR summary :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

verifyCmd.Flags().StringP("token", "t", "", "Token to verify")
verifyCmd.Flags().StringP("output", "o", "", "Output format. Only `json` is supported currently.")
viper.BindPFlag("token", verifyCmd.Flags().Lookup("token"))
Expand Down
21 changes: 18 additions & 3 deletions pkg/token/token.go
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/aws/aws-sdk-go/service/sts/stsiface"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/pkg/apis/clientauthentication"
clientauthv1beta1 "k8s.io/client-go/pkg/apis/clientauthentication/v1beta1"
"sigs.k8s.io/aws-iam-authenticator/pkg"
"sigs.k8s.io/aws-iam-authenticator/pkg/arn"
Expand Down Expand Up @@ -89,7 +90,9 @@ const (
clusterIDHeader = "x-k8s-aws-id"
// Format of the X-Amz-Date header used for expiration
// https://golang.org/pkg/time/#pkg-constants
dateHeaderFormat = "20060102T150405Z"
dateHeaderFormat = "20060102T150405Z"
kindExecCredential = "ExecCredential"
execInfoEnvKey = "KUBERNETES_EXEC_INFO"
)

// Token is generated and used by Kubernetes client-go to authenticate with a Kubernetes cluster.
Expand Down Expand Up @@ -338,11 +341,23 @@ func (g generator) GetWithSTS(clusterID string, stsAPI stsiface.STSAPI) (Token,

// FormatJSON formats the json to support ExecCredential authentication
func (g generator) FormatJSON(token Token) string {
apiVersion := clientauthv1beta1.SchemeGroupVersion.String()
for _, e := range os.Environ() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't parse this manually, instead use LoadExecCredentialFromEnv(). Example usage:

package main

import (
	"fmt"
	"k8s.io/client-go/tools/auth/exec"
)

func main() {
	obj, config, err := exec.LoadExecCredentialFromEnv()
	fmt.Printf("%#v, %#v, %#v\n", obj, config, err)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice. Thank you!!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like https://github.com/kubernetes/client-go/blob/master/pkg/apis/clientauthentication/types.go#L54-L59 is sending an error ExecCredential does not contain cluster information .. We have to keep this implementation and work on fixing the error in client-go. wdyt?

pair := strings.SplitN(e, "=", 2)
if pair[0] == execInfoEnvKey {
cred := &clientauthentication.ExecCredential{}
if err := json.Unmarshal([]byte(pair[1]), cred); err == nil {
apiVersion = cred.APIVersion
}
break
}
}

expirationTimestamp := metav1.NewTime(token.Expiration)
execInput := &clientauthv1beta1.ExecCredential{
TypeMeta: metav1.TypeMeta{
APIVersion: "client.authentication.k8s.io/v1beta1",
Kind: "ExecCredential",
APIVersion: apiVersion,
Kind: kindExecCredential,
},
Status: &clientauthv1beta1.ExecCredentialStatus{
ExpirationTimestamp: &expirationTimestamp,
Expand Down
89 changes: 89 additions & 0 deletions pkg/token/token_test.go
Expand Up @@ -10,11 +10,17 @@ import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"

"github.com/prometheus/client_golang/prometheus"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/pkg/apis/clientauthentication"
clientauthv1 "k8s.io/client-go/pkg/apis/clientauthentication/v1"
clientauthv1alpha1 "k8s.io/client-go/pkg/apis/clientauthentication/v1alpha1"
clientauthv1beta1 "k8s.io/client-go/pkg/apis/clientauthentication/v1beta1"
"sigs.k8s.io/aws-iam-authenticator/pkg/metrics"
)

Expand Down Expand Up @@ -318,3 +324,86 @@ func TestVerifyCanonicalARN(t *testing.T) {
t.Errorf("expected CannonicalARN to be %q but was %q", canonicalARN, identity.CanonicalARN)
}
}

func TestFormatJson(t *testing.T) {
cases := []struct {
Name string
EnvKey string
ExpectApiVersion string
IsMalformedEnv bool
}{
{
Name: "Default",
ExpectApiVersion: clientauthv1beta1.SchemeGroupVersion.String(),
},
{
Name: "Malformed KUBERNETES_EXEC_INFO",
EnvKey: "KUBERNETES_EXEC_INFO",
ExpectApiVersion: clientauthv1beta1.SchemeGroupVersion.String(),
jyotimahapatra marked this conversation as resolved.
Show resolved Hide resolved
},
{
Name: "Malformed KUBERNETES_EXEC_INFO",
EnvKey: "KUBERNETES_EXEC_INFO",
IsMalformedEnv: true,
ExpectApiVersion: clientauthv1beta1.SchemeGroupVersion.String(),
},
{
Name: "KUBERNETES_EXEC_INFO with v1beta1",
EnvKey: "KUBERNETES_EXEC_INFO",
ExpectApiVersion: clientauthv1beta1.SchemeGroupVersion.String(),
},
{
Name: "KUBERNETES_EXEC_INFO with v1alpha1",
EnvKey: "KUBERNETES_EXEC_INFO",
ExpectApiVersion: clientauthv1alpha1.SchemeGroupVersion.String(),
},
{
Name: "KUBERNETES_EXEC_INFO with v1",
EnvKey: "KUBERNETES_EXEC_INFO",
ExpectApiVersion: clientauthv1.SchemeGroupVersion.String(),
},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
expiry, _ := time.Parse(time.RFC3339, "2012-11-01T22:08:41+00:00")
token := "token"
g, _ := NewGenerator(true, true)

if c.EnvKey != "" {
marshal := make([]byte, 0)
if !c.IsMalformedEnv {
marshal, _ = json.Marshal(clientauthentication.ExecCredential{
TypeMeta: v1.TypeMeta{
Kind: "ExecCredential",
APIVersion: c.ExpectApiVersion,
},
})
}

os.Setenv(c.EnvKey, string(marshal))
}

jsonResponse := g.FormatJSON(Token{Token: token, Expiration: expiry})
output := &clientauthentication.ExecCredential{}
json.Unmarshal([]byte(jsonResponse), output)

if output.TypeMeta.Kind != kindExecCredential {
t.Errorf("expected Kind to be %s but was %s", kindExecCredential, output.TypeMeta.Kind)
}

if output.TypeMeta.APIVersion != c.ExpectApiVersion {
t.Errorf("expected APIVersion to be %s but was %s", c.ExpectApiVersion, output.TypeMeta.APIVersion)
}

if output.Status.Token != token {
t.Errorf("expected token to be %s but was %s", token, output.Status.Token)
}

if !output.Status.ExpirationTimestamp.Time.Equal(expiry) {
t.Errorf("expected expiration to be %s but was %s", expiry, output.Status.ExpirationTimestamp)
}

os.Unsetenv(c.EnvKey)
})
}
}