-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
50491a1
commit 1f95113
Showing
30 changed files
with
1,288 additions
and
349 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,4 @@ Dockerfile.cross | |
# Ignore DS_Store files | ||
.DS_Store | ||
**/.DS_Store | ||
dex-operator |
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,52 @@ | ||
package app | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/giantswarm/dex-operator/pkg/key" | ||
|
||
"github.com/giantswarm/apiextensions-application/api/v1alpha1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type Config struct { | ||
RedirectURI string | ||
Name string | ||
IssuerURI string | ||
IdentifierURI string | ||
SecretValidityMonths int | ||
} | ||
|
||
type ManagementClusterProps struct { | ||
Name string | ||
BaseDomain string | ||
IssuerAddress string | ||
} | ||
|
||
func GetConfig(ctx context.Context, app *v1alpha1.App, client client.Client, managementCluster ManagementClusterProps) (Config, error) { | ||
var baseDomain string | ||
|
||
// Get the cluster values configmap if present (workload cluster format) | ||
if ClusterValuesIsPresent(app) { | ||
clusterValuesConfigmap := &corev1.ConfigMap{} | ||
if err := client.Get(ctx, types.NamespacedName{ | ||
Name: app.Spec.Config.ConfigMap.Name, | ||
Namespace: app.Spec.Config.ConfigMap.Namespace}, | ||
clusterValuesConfigmap); err != nil { | ||
return Config{}, err | ||
} | ||
// Get the base domain | ||
baseDomain = GetBaseDomainFromClusterValues(clusterValuesConfigmap) | ||
} | ||
issuerAddress := GetIssuerAddress(baseDomain, managementCluster.IssuerAddress, managementCluster.BaseDomain) | ||
|
||
return Config{ | ||
Name: key.GetIdpAppName(managementCluster.Name, app.Namespace, app.Name), | ||
IssuerURI: key.GetIssuerURI(issuerAddress), | ||
RedirectURI: key.GetRedirectURI(issuerAddress), | ||
IdentifierURI: key.GetIdentifierURI(key.GetIdpAppName(managementCluster.Name, app.Namespace, app.Name)), | ||
SecretValidityMonths: key.SecretValidityMonths, | ||
}, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package app | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/giantswarm/dex-operator/pkg/key" | ||
"github.com/giantswarm/dex-operator/pkg/tests" | ||
|
||
"github.com/giantswarm/apiextensions-application/api/v1alpha1" | ||
corev1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
) | ||
|
||
func TestGetAppConfig(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
managementClusterName string | ||
managementClusterBaseDomain string | ||
managementClusterIssuerAddress string | ||
app *v1alpha1.App | ||
clusterValuesConfigMap *corev1.ConfigMap | ||
expectedAppConfig Config | ||
}{ | ||
{ | ||
name: "case 0: Get issuer URL from cluster config values", | ||
managementClusterName: "testcluster", | ||
managementClusterBaseDomain: "base.domain.io", | ||
managementClusterIssuerAddress: "issuer.cluster.base.domain.io", | ||
app: tests.GetExampleApp(), | ||
clusterValuesConfigMap: getClusterValuesConfigMap("baseDomain: wc.cluster.domain.io"), | ||
expectedAppConfig: Config{ | ||
Name: "testcluster-example-test", | ||
IssuerURI: "https://dex.wc.cluster.domain.io", | ||
RedirectURI: "https://dex.wc.cluster.domain.io/callback", | ||
IdentifierURI: "https://dex.giantswarm.io/testcluster-example-test", | ||
SecretValidityMonths: key.SecretValidityMonths, | ||
}, | ||
}, | ||
{ | ||
name: "case 1: Get issuer URL from management cluster issuer URL property", | ||
managementClusterName: "testcluster", | ||
managementClusterBaseDomain: "base.domain.io", | ||
managementClusterIssuerAddress: "issuer.cluster.domain.io", | ||
app: tests.GetExampleApp(), | ||
expectedAppConfig: Config{ | ||
Name: "testcluster-example-test", | ||
IssuerURI: "https://issuer.cluster.domain.io", | ||
RedirectURI: "https://issuer.cluster.domain.io/callback", | ||
IdentifierURI: "https://dex.giantswarm.io/testcluster-example-test", | ||
SecretValidityMonths: key.SecretValidityMonths, | ||
}, | ||
}, | ||
{ | ||
name: "case 2: Get issuer URL from management cluster base domain", | ||
managementClusterName: "testcluster", | ||
managementClusterBaseDomain: "base.domain.io", | ||
app: tests.GetExampleApp(), | ||
expectedAppConfig: Config{ | ||
Name: "testcluster-example-test", | ||
IssuerURI: "https://dex.g8s.base.domain.io", | ||
RedirectURI: "https://dex.g8s.base.domain.io/callback", | ||
IdentifierURI: "https://dex.giantswarm.io/testcluster-example-test", | ||
SecretValidityMonths: key.SecretValidityMonths, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
|
||
ctx := context.Background() | ||
|
||
fakeClientBuilder := fake.NewClientBuilder() | ||
if tc.clusterValuesConfigMap != nil { | ||
tc.app.Spec = v1alpha1.AppSpec{ | ||
Config: v1alpha1.AppSpecConfig{ | ||
ConfigMap: v1alpha1.AppSpecConfigConfigMap{ | ||
Name: tc.clusterValuesConfigMap.Name, | ||
Namespace: tc.clusterValuesConfigMap.Namespace, | ||
}, | ||
}, | ||
} | ||
fakeClientBuilder.WithObjects(tc.clusterValuesConfigMap) | ||
} | ||
|
||
appConfig, err := GetConfig(ctx, tc.app, fakeClientBuilder.Build(), ManagementClusterProps{ | ||
Name: tc.managementClusterName, | ||
BaseDomain: tc.managementClusterBaseDomain, | ||
IssuerAddress: tc.managementClusterIssuerAddress, | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !reflect.DeepEqual(appConfig, tc.expectedAppConfig) { | ||
t.Fatalf("Expacted %v, got %v", tc.expectedAppConfig, appConfig) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func getClusterValuesConfigMap(clusterValues string) *corev1.ConfigMap { | ||
name := fmt.Sprintf("test-%s", key.ClusterValuesConfigmapSuffix) | ||
return tests.GetClusterValuesConfigMap(name, "example", clusterValues) | ||
} |
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,61 @@ | ||
package app | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/giantswarm/dex-operator/pkg/key" | ||
|
||
"github.com/giantswarm/apiextensions-application/api/v1alpha1" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func RemoveExtraConfig(extraConfigs []v1alpha1.AppExtraConfig, extraConfig v1alpha1.AppExtraConfig) []v1alpha1.AppExtraConfig { | ||
if extraConfigs == nil { | ||
return extraConfigs | ||
} | ||
result := []v1alpha1.AppExtraConfig{} | ||
for _, config := range extraConfigs { | ||
if !reflect.DeepEqual(config, extraConfig) { | ||
result = append(result, config) | ||
} | ||
} | ||
return result | ||
} | ||
|
||
func ClusterValuesIsPresent(app *v1alpha1.App) bool { | ||
return strings.HasSuffix(app.Spec.Config.ConfigMap.Name, key.ClusterValuesConfigmapSuffix) | ||
} | ||
|
||
func GetIssuerAddress(baseDomain string, managementClusterIssuerAddress string, managementClusterBaseDomain string) string { | ||
var issuerAddress string | ||
{ | ||
// Derive issuer address from cluster basedomain if it exists | ||
if baseDomain != "" { | ||
issuerAddress = key.GetIssuerAddress(baseDomain) | ||
} | ||
|
||
// Otherwise fall back to management cluster issuer address if present | ||
if issuerAddress == "" { | ||
issuerAddress = managementClusterIssuerAddress | ||
} | ||
|
||
// If all else fails, fall back to the base domain (only works in vintage) | ||
if issuerAddress == "" { | ||
clusterDomain := key.GetVintageClusterDomain(managementClusterBaseDomain) | ||
issuerAddress = key.GetIssuerAddress(clusterDomain) | ||
} | ||
} | ||
return issuerAddress | ||
} | ||
|
||
func GetBaseDomainFromClusterValues(clusterValuesConfigmap *corev1.ConfigMap) string { | ||
values := clusterValuesConfigmap.Data[key.ValuesConfigMapKey] | ||
rex := regexp.MustCompile(fmt.Sprintf(`(%v)(\s*:\s*)(\S+)`, key.BaseDomainKey)) | ||
if matches := rex.FindStringSubmatch(values); len(matches) > 3 { | ||
return matches[3] | ||
} | ||
return "" | ||
} |
Oops, something went wrong.