-
Notifications
You must be signed in to change notification settings - Fork 5
/
auth.go
36 lines (29 loc) · 886 Bytes
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package api
import (
"fmt"
"github.com/metal-stack/metal-lib/auth"
)
const CloudContext = "metalctl"
// getAuthContext reads AuthContext from given kubeconfig
func GetAuthContext(kubeconfig string) (*auth.AuthContext, error) {
cs, err := GetContexts()
if err != nil {
return nil, err
}
authContext, err := auth.GetAuthContext(kubeconfig, FormatContextName(CloudContext, cs.CurrentContext))
if err != nil {
return nil, err
}
if !authContext.AuthProviderOidc {
return nil, fmt.Errorf("active user %s has no oidc authProvider, check config", authContext.User)
}
return &authContext, nil
}
// formatContextName returns the contextName for the given suffix. suffix can be empty.
func FormatContextName(prefix string, suffix string) string {
contextName := prefix
if suffix != "" {
contextName = fmt.Sprintf("%s-%s", CloudContext, suffix)
}
return contextName
}