Skip to content
This repository has been archived by the owner on Mar 11, 2021. It is now read-only.

Add authorization checking to Kubernetes package #2207

Merged
merged 12 commits into from
Sep 10, 2018
15 changes: 6 additions & 9 deletions controller/deployments_urlprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,23 +199,20 @@ func (up *tenantURLProvider) GetEnvironmentMapping() map[string]string {
log.Error(nil, map[string]interface{}{
"namespace": envNS,
}, "namespace has no type")
} else if !isInternalNamespace(*envName) {
} else {
result[*envName] = envNS
}
}
return result
}

// Types of namespaces where the user does not deploy applications
var internalNamespaceTypes = []string{"user", "che", "jenkins"}
var internalNamespaceTypes = map[string]struct{}{"user": {}, "che": {}, "jenkins": {}}

func isInternalNamespace(envType string) bool {
for _, internalType := range internalNamespaceTypes {
if envType == internalType {
return true
}
}
return false
// CanDeploy returns true if the environment type provided can be deployed to as part of a pipeline
func (up *tenantURLProvider) CanDeploy(envType string) bool {
_, pres := internalNamespaceTypes[envType]
return !pres
}

func (up *tenantURLProvider) GetAPIToken() (*string, error) {
Expand Down
40 changes: 36 additions & 4 deletions controller/deployments_urlprovider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,22 +225,31 @@ func TestTenantGetEnvironmentMapping(t *testing.T) {
testName: "Basic",
inputFile: "user-services.json",
expectedMap: map[string]string{
"run": "theuser-run",
"stage": "theuser-stage",
"user": "theuser",
"run": "theuser-run",
"stage": "theuser-stage",
"che": "theuser-che",
"jenkins": "theuser-jenkins",
},
},
{
testName: "No Type",
inputFile: "user-services-no-type.json",
expectedMap: map[string]string{
"run": "theuser-run",
"user": "theuser",
"run": "theuser-run",
"che": "theuser-che",
"jenkins": "theuser-jenkins",
},
},
{
testName: "Empty Type",
inputFile: "user-services-empty-type.json",
expectedMap: map[string]string{
"run": "theuser-run",
"user": "theuser",
"run": "theuser-run",
"che": "theuser-che",
"jenkins": "theuser-jenkins",
},
},
}
Expand All @@ -259,6 +268,29 @@ func TestTenantGetEnvironmentMapping(t *testing.T) {
}
}

func TestTenantCanDeploy(t *testing.T) {
testCases := []struct {
envType string
expected bool
}{
{"user", false},
{"test", true},
{"stage", true},
{"run", true},
{"che", false},
{"jenkins", false},
}

for _, testCase := range testCases {
t.Run(testCase.envType, func(t *testing.T) {
provider, err := getDefaultTenantProvider()
require.NoError(t, err)
result := provider.CanDeploy(testCase.envType)
require.Equal(t, testCase.expected, result, "Incorrect result from CanDeploy")
})
}
}

//////////////////////////////////////////////////////////////////////////////////////////////////

func tostring(item interface{}) string {
Expand Down
Loading