-
Notifications
You must be signed in to change notification settings - Fork 705
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 #42 from ngtuna/gke-permission
binding active account to the `cluster-admin` role in GKE
- Loading branch information
Showing
43 changed files
with
6,821 additions
and
8 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 |
---|---|---|
|
@@ -49,6 +49,7 @@ var downCmd = &cobra.Command{ | |
if err != nil { | ||
return err | ||
} | ||
|
||
return c.Run(objs) | ||
}, | ||
} | ||
|
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,63 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"k8s.io/apimachinery/pkg/version" | ||
"k8s.io/client-go/discovery" | ||
restclient "k8s.io/client-go/rest" | ||
) | ||
|
||
func TestIsGKE(t *testing.T) { | ||
gkeVersion := version.Info{ | ||
Major: "foo", | ||
Minor: "bar", | ||
GitVersion: "foobar-gke", | ||
} | ||
|
||
nonGkeVersion := version.Info{ | ||
Major: "foo", | ||
Minor: "bar", | ||
GitVersion: "baz", | ||
} | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
output, err := json.Marshal(gkeVersion) | ||
if err != nil { | ||
t.Errorf("unexpected encoding error: %v", err) | ||
return | ||
} | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
w.Write(output) | ||
})) | ||
defer server.Close() | ||
client := discovery.NewDiscoveryClientForConfigOrDie(&restclient.Config{Host: server.URL}) | ||
|
||
if ok, err := isGKE(client); err != nil { | ||
t.Error(err) | ||
} else if !ok { | ||
t.Errorf("expect GKE but got non-GKE") | ||
} | ||
|
||
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
output, err := json.Marshal(nonGkeVersion) | ||
if err != nil { | ||
t.Errorf("unexpected encoding error: %v", err) | ||
return | ||
} | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
w.Write(output) | ||
})) | ||
defer server.Close() | ||
client = discovery.NewDiscoveryClientForConfigOrDie(&restclient.Config{Host: server.URL}) | ||
|
||
if ok, err := isGKE(client); err != nil { | ||
t.Error(err) | ||
} else if ok { | ||
t.Errorf("expect non-GKE but got GKE") | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,96 @@ | ||
package gke | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"os/user" | ||
"path/filepath" | ||
"runtime" | ||
|
||
"github.com/go-ini/ini" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
) | ||
|
||
// BuildCrbObject builds the clusterrolebinding for granting | ||
// cluster-admin permission to the active user. | ||
func BuildCrbObject(user string) ([]*unstructured.Unstructured, error) { | ||
crb := &unstructured.Unstructured{ | ||
Object: map[string]interface{}{ | ||
"kind": "ClusterRoleBinding", | ||
"apiVersion": "rbac.authorization.k8s.io/v1beta1", | ||
"metadata": map[string]interface{}{ | ||
"name": "kubeapps-cluster-admin", | ||
}, | ||
"roleRef": map[string]interface{}{ | ||
"apiGroup": "rbac.authorization.k8s.io", | ||
"kind": "ClusterRole", | ||
"name": "cluster-admin", | ||
}, | ||
"subjects": []map[string]interface{}{ | ||
{ | ||
"apiGroup": "rbac.authorization.k8s.io", | ||
"kind": "User", | ||
"name": user, | ||
}, | ||
}, | ||
}, | ||
} | ||
crbs := []*unstructured.Unstructured{} | ||
crbs = append(crbs, crb) | ||
return crbs, nil | ||
} | ||
|
||
// GetActiveUser returns user logging to gcloud | ||
func GetActiveUser(gcloudPath string) (string, error) { | ||
activeConfigPath := filepath.Join(gcloudPath, "active_config") | ||
activeConfig, err := ioutil.ReadFile(activeConfigPath) | ||
if err != nil { | ||
return "", fmt.Errorf("can't read file active_config: %v", err) | ||
} | ||
|
||
configPath := filepath.Join(gcloudPath, "configurations") | ||
if _, err := os.Stat(filepath.Join(configPath, "config_"+string(activeConfig))); os.IsNotExist(err) { | ||
return "", fmt.Errorf("the config file for active_config doesn't exist: %v", err) | ||
} | ||
|
||
cfg, err := ini.Load(filepath.Join(configPath, "config_"+string(activeConfig))) | ||
if err != nil { | ||
return "", fmt.Errorf("can't load file for the active_config: %v", err) | ||
} | ||
|
||
core, err := cfg.GetSection("core") | ||
if err != nil { | ||
return "", fmt.Errorf("can't get section [core]: %v", err) | ||
} | ||
account, err := core.GetKey("account") | ||
if err != nil { | ||
return "", fmt.Errorf("can't get key account: %v", err) | ||
} | ||
|
||
return account.Value(), nil | ||
} | ||
|
||
// SdkConfigPath returns path to gcloud sdk config | ||
var SdkConfigPath = func() (string, error) { | ||
if runtime.GOOS == "windows" { | ||
return filepath.Join(os.Getenv("APPDATA"), "gcloud"), nil | ||
} | ||
homeDir := guessUnixHomeDir() | ||
if homeDir == "" { | ||
return "", fmt.Errorf("unable to get current user home directory: os/user lookup failed; $HOME is empty") | ||
} | ||
return filepath.Join(homeDir, ".config", "gcloud"), nil | ||
} | ||
|
||
func guessUnixHomeDir() string { | ||
// Prefer $HOME over user.Current due to glibc bug: golang.org/issue/13470 | ||
if v := os.Getenv("HOME"); v != "" { | ||
return v | ||
} | ||
// Else, fall back to user.Current: | ||
if u, err := user.Current(); err == nil { | ||
return u.HomeDir | ||
} | ||
return "" | ||
} |
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,51 @@ | ||
package gke | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
const ( | ||
SUCCESS_PATH = "testsdkconfig/success" | ||
FAILURE_PATH = "testsdkconfig/failure" | ||
) | ||
|
||
func TestGetActiveUserSuccess(t *testing.T) { | ||
email, err := GetActiveUser(SUCCESS_PATH) | ||
if err != nil { | ||
t.Error() | ||
} | ||
if email != "foo@example.com" { | ||
t.Errorf("expected email = foo@example.com, got email = %s", email) | ||
} | ||
} | ||
|
||
func TestGetActiveUserFailure(t *testing.T) { | ||
email, err := GetActiveUser(FAILURE_PATH) | ||
if err == nil { | ||
t.Error() | ||
} | ||
if email != "" { | ||
t.Errorf("expected email not found, got email = %s", email) | ||
} | ||
} | ||
|
||
func TestBuildCrbObject(t *testing.T) { | ||
user := "foo" | ||
crd, err := BuildCrbObject(user) | ||
if err != nil { | ||
t.Errorf("expected crb object can be built successfully, got error %v", err) | ||
} | ||
if len(crd) != 1 { | ||
t.Errorf("expected build a single srb, got %d elements", len(crd)) | ||
} | ||
|
||
data := crd[0].UnstructuredContent() | ||
sub := data["subjects"].([]map[string]interface{}) | ||
if len(sub) != 1 { | ||
t.Errorf("expected to have only one subject, got %v", len(sub)) | ||
} | ||
|
||
if sub[0]["name"].(string) != user { | ||
t.Errorf("expected user %s should be loaded into crb object, got %s", user, sub[0]["name"].(string)) | ||
} | ||
} |
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 @@ | ||
test |
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,5 @@ | ||
[core] | ||
foo = bar | ||
|
||
[compute] | ||
bar = baz |
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 @@ | ||
test |
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,7 @@ | ||
[core] | ||
account = foo@example.com | ||
foo = bar | ||
|
||
[baz] | ||
bar = baz | ||
foo = bar |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
vendor/github.com/go-ini/ini/.github/PULL_REQUEST_TEMPLATE.md
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.