-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from nmaupu/use-client-jwt-k8s
Using client's service account to make a k8s auth
- Loading branch information
Showing
8 changed files
with
111 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,8 @@ spec: | |
type: string | ||
role: | ||
type: string | ||
serviceAccount: | ||
type: string | ||
required: | ||
- cluster | ||
- role | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ rules: | |
- events | ||
- configmaps | ||
- secrets | ||
- serviceaccounts | ||
verbs: | ||
- '*' | ||
- apiGroups: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package k8sutils | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// GetTokenFromSA gets the token associated to the first secret located in a k8s' service account | ||
func GetTokenFromSA(cli client.Client, ns, saName string) (string, error) { | ||
if cli == nil { | ||
return "", fmt.Errorf("Cannot get token from service account, k8s client is nil") | ||
} | ||
|
||
// Getting SA | ||
saClient := &corev1.ServiceAccount{} | ||
err := cli.Get(context.TODO(), types.NamespacedName{Name: saName, Namespace: ns}, saClient) | ||
if err != nil && errors.IsNotFound(err) { | ||
return "", fmt.Errorf("Unable to retrieve service account, err=%v", err) | ||
} | ||
|
||
if len(saClient.Secrets) == 0 { | ||
return "", fmt.Errorf("No secret associated with the service account %s/%s", ns, saName) | ||
} | ||
|
||
// TODO See how to handle this slice of Secrets instead of taking the first one | ||
saSecret := saClient.Secrets[0] | ||
secret := &corev1.Secret{} | ||
err = cli.Get(context.TODO(), types.NamespacedName{Name: saSecret.Name, Namespace: ns}, secret) | ||
if err != nil { | ||
return "", fmt.Errorf("Unable to retrieve the secret from the service account, err=%v", err) | ||
} | ||
|
||
// Finally, set the token | ||
return string(secret.Data["token"]), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters