Skip to content

Commit

Permalink
Dry run of OAuth patch
Browse files Browse the repository at this point in the history
  • Loading branch information
jhadvig committed Jan 15, 2020
1 parent ca9b90b commit 07f558e
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 93 deletions.
33 changes: 22 additions & 11 deletions frontend/public/components/cluster-settings/basicauth-idp-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ActionGroup, Button } from '@patternfly/react-core';
import { SecretModel, ConfigMapModel } from '../../models';
import { IdentityProvider, k8sCreate, K8sResourceKind, OAuthKind } from '../../module/k8s';
import { ButtonBar, PromiseComponent, history, AsyncComponent } from '../utils';
import { addIDP, getOAuthResource, redirectToOAuthPage } from './';
import { addIDP, getOAuthResource, redirectToOAuthPage, mockNames } from './';
import { IDPNameInput } from './idp-name-input';
import { IDPCAFileInput } from './idp-cafile-input';

Expand Down Expand Up @@ -75,7 +75,12 @@ export class AddBasicAuthPage extends PromiseComponent<{}, AddBasicAuthPageState
return this.handlePromise(k8sCreate(ConfigMapModel, ca));
}

addBasicAuthIDP(oauth: OAuthKind, secretName: string, caName: string): Promise<K8sResourceKind> {
addBasicAuthIDP(
oauth: OAuthKind,
secretName: string,
caName: string,
dryRun?: boolean,
): Promise<K8sResourceKind> {
const { name, url } = this.state;
const idp: IdentityProvider = {
name,
Expand All @@ -101,7 +106,7 @@ export class AddBasicAuthPage extends PromiseComponent<{}, AddBasicAuthPageState
};
}

return this.handlePromise(addIDP(oauth, idp));
return this.handlePromise(addIDP(oauth, idp, dryRun));
}

submit: React.FormEventHandler<HTMLFormElement> = (e) => {
Expand All @@ -115,15 +120,21 @@ export class AddBasicAuthPage extends PromiseComponent<{}, AddBasicAuthPageState
// Clear any previous errors.
this.setState({ errorMessage: '' });
this.getOAuthResource().then((oauth: OAuthKind) => {
const promises = [this.createTLSSecret(), this.createCAConfigMap()];

Promise.all(promises)
.then(([tlsSecret, configMap]) => {
const caName = configMap ? configMap.metadata.name : '';
const secretName = tlsSecret ? tlsSecret.metadata.name : '';
return this.addBasicAuthIDP(oauth, secretName, caName);
this.addBasicAuthIDP(oauth, mockNames.secret, mockNames.ca, true)
.then(() => {
const promises = [this.createTLSSecret(), this.createCAConfigMap()];

Promise.all(promises)
.then(([tlsSecret, configMap]) => {
const caName = configMap ? configMap.metadata.name : '';
const secretName = tlsSecret ? tlsSecret.metadata.name : '';
return this.addBasicAuthIDP(oauth, secretName, caName);
})
.then(redirectToOAuthPage);
})
.then(redirectToOAuthPage);
.catch((err) => {
this.setState({ errorMessage: err });
});
});
};

Expand Down
23 changes: 15 additions & 8 deletions frontend/public/components/cluster-settings/github-idp-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ActionGroup, Button } from '@patternfly/react-core';
import { SecretModel, ConfigMapModel } from '../../models';
import { IdentityProvider, k8sCreate, K8sResourceKind, OAuthKind } from '../../module/k8s';
import { ButtonBar, ListInput, PromiseComponent, history } from '../utils';
import { addIDP, getOAuthResource, redirectToOAuthPage } from './';
import { addIDP, getOAuthResource, redirectToOAuthPage, mockNames } from './';
import { IDPNameInput } from './idp-name-input';
import { IDPCAFileInput } from './idp-cafile-input';

Expand Down Expand Up @@ -68,6 +68,7 @@ export class AddGitHubPage extends PromiseComponent<{}, AddGitHubPageState> {
oauth: OAuthKind,
clientSecretName: string,
caName: string,
dryRun?: boolean,
): Promise<K8sResourceKind> {
const { name, clientID, hostname, organizations, teams } = this.state;
const idp: IdentityProvider = {
Expand All @@ -91,7 +92,7 @@ export class AddGitHubPage extends PromiseComponent<{}, AddGitHubPageState> {
};
}

return this.handlePromise(addIDP(oauth, idp));
return this.handlePromise(addIDP(oauth, idp, dryRun));
}

submit: React.FormEventHandler<HTMLFormElement> = (e) => {
Expand All @@ -104,14 +105,20 @@ export class AddGitHubPage extends PromiseComponent<{}, AddGitHubPageState> {
// Clear any previous errors.
this.setState({ errorMessage: '' });
this.getOAuthResource().then((oauth: OAuthKind) => {
const promises = [this.createClientSecret(), this.createCAConfigMap()];
this.addGitHubIDP(oauth, mockNames.secret, mockNames.ca, true)
.then(() => {
const promises = [this.createClientSecret(), this.createCAConfigMap()];

Promise.all(promises)
.then(([secret, configMap]) => {
const caName = configMap ? configMap.metadata.name : '';
return this.addGitHubIDP(oauth, secret.metadata.name, caName);
Promise.all(promises)
.then(([secret, configMap]) => {
const caName = configMap ? configMap.metadata.name : '';
return this.addGitHubIDP(oauth, secret.metadata.name, caName);
})
.then(redirectToOAuthPage);
})
.then(redirectToOAuthPage);
.catch((err) => {
this.setState({ errorMessage: err });
});
});
};

Expand Down
29 changes: 6 additions & 23 deletions frontend/public/components/cluster-settings/gitlab-idp-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@ import * as React from 'react';
import { Helmet } from 'react-helmet';
import { ActionGroup, Button } from '@patternfly/react-core';

import { SecretModel, ConfigMapModel, OAuthModel } from '../../models';
import {
IdentityProvider,
k8sCreate,
K8sResourceKind,
OAuthKind,
k8sPatch,
} from '../../module/k8s';
import { SecretModel, ConfigMapModel } from '../../models';
import { IdentityProvider, k8sCreate, K8sResourceKind, OAuthKind } from '../../module/k8s';
import { ButtonBar, PromiseComponent, history } from '../utils';
import { addIDP, getOAuthResource, redirectToOAuthPage } from './';
import { addIDP, getOAuthResource, redirectToOAuthPage, mockNames } from './';
import { IDPNameInput } from './idp-name-input';
import { IDPCAFileInput } from './idp-cafile-input';
import { dryRunOpt } from '@console/dev-console/src/utils/shared-submit-utils';

export class AddGitLabPage extends PromiseComponent<{}, AddGitLabPageState> {
readonly state: AddGitLabPageState = {
Expand Down Expand Up @@ -73,6 +66,7 @@ export class AddGitLabPage extends PromiseComponent<{}, AddGitLabPageState> {
oauth: OAuthKind,
clientSecretName: string,
caName: string,
dryRun?: boolean,
): Promise<K8sResourceKind> {
const { name, clientID, url } = this.state;
const idp: IdentityProvider = {
Expand All @@ -94,18 +88,7 @@ export class AddGitLabPage extends PromiseComponent<{}, AddGitLabPageState> {
};
}

return this.handlePromise(addIDP(oauth, idp));
}

dryCreateOAuth(oauth): Promise<K8sResourceKind> {
const patch = [
{
op: 'add',
path: 'metadata/annotations',
value: { key: 'value' },
},
];
return this.handlePromise(k8sPatch(OAuthModel, oauth, patch, dryRunOpt));
return this.handlePromise(addIDP(oauth, idp, dryRun));
}

submit: React.FormEventHandler<HTMLFormElement> = (e) => {
Expand All @@ -114,7 +97,7 @@ export class AddGitLabPage extends PromiseComponent<{}, AddGitLabPageState> {
// Clear any previous errors.
this.setState({ errorMessage: '' });
this.getOAuthResource().then((oauth: OAuthKind) => {
this.dryCreateOAuth(oauth)
this.addGitLabIDP(oauth, mockNames.secret, mockNames.ca, true)
.then(() => {
const promises = [this.createClientSecret(), this.createCAConfigMap()];

Expand Down
22 changes: 16 additions & 6 deletions frontend/public/components/cluster-settings/google-idp-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ActionGroup, Button } from '@patternfly/react-core';
import { SecretModel } from '../../models';
import { IdentityProvider, k8sCreate, K8sResourceKind, OAuthKind } from '../../module/k8s';
import { ButtonBar, PromiseComponent, history } from '../utils';
import { addIDP, getOAuthResource, redirectToOAuthPage } from './';
import { addIDP, getOAuthResource, redirectToOAuthPage, mockNames } from './';
import { IDPNameInput } from './idp-name-input';

export class AddGooglePage extends PromiseComponent<{}, AddGooglePageState> {
Expand Down Expand Up @@ -39,7 +39,11 @@ export class AddGooglePage extends PromiseComponent<{}, AddGooglePageState> {
return this.handlePromise(k8sCreate(SecretModel, secret));
}

addGoogleIDP(oauth: OAuthKind, clientSecretName: string): Promise<K8sResourceKind> {
addGoogleIDP(
oauth: OAuthKind,
clientSecretName: string,
dryRun?: boolean,
): Promise<K8sResourceKind> {
const { name, clientID, hostedDomain } = this.state;
const idp: IdentityProvider = {
name,
Expand All @@ -54,7 +58,7 @@ export class AddGooglePage extends PromiseComponent<{}, AddGooglePageState> {
},
};

return this.handlePromise(addIDP(oauth, idp));
return this.handlePromise(addIDP(oauth, idp, dryRun));
}

submit: React.FormEventHandler<HTMLFormElement> = (e) => {
Expand All @@ -63,9 +67,15 @@ export class AddGooglePage extends PromiseComponent<{}, AddGooglePageState> {
// Clear any previous errors.
this.setState({ errorMessage: '' });
this.getOAuthResource().then((oauth: OAuthKind) => {
return this.createClientSecret()
.then((secret: K8sResourceKind) => this.addGoogleIDP(oauth, secret.metadata.name))
.then(redirectToOAuthPage);
this.addGoogleIDP(oauth, mockNames.secret, true)
.then(() => {
return this.createClientSecret()
.then((secret: K8sResourceKind) => this.addGoogleIDP(oauth, secret.metadata.name))
.then(redirectToOAuthPage);
})
.catch((err) => {
this.setState({ errorMessage: err });
});
});
};

Expand Down
18 changes: 12 additions & 6 deletions frontend/public/components/cluster-settings/htpasswd-idp-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ActionGroup, Button } from '@patternfly/react-core';
import { SecretModel } from '../../models';
import { IdentityProvider, k8sCreate, K8sResourceKind, OAuthKind } from '../../module/k8s';
import { AsyncComponent, ButtonBar, PromiseComponent, history } from '../utils';
import { addIDP, getOAuthResource, redirectToOAuthPage } from './';
import { addIDP, getOAuthResource, redirectToOAuthPage, mockNames } from './';
import { IDPNameInput } from './idp-name-input';

export const DroppableFileInput = (props: any) => (
Expand Down Expand Up @@ -43,7 +43,7 @@ export class AddHTPasswdPage extends PromiseComponent<{}, AddHTPasswdPageState>
return this.handlePromise(k8sCreate(SecretModel, secret));
}

addHTPasswdIDP(oauth: OAuthKind, secretName: string): Promise<K8sResourceKind> {
addHTPasswdIDP(oauth: OAuthKind, secretName: string, dryRun?: boolean): Promise<K8sResourceKind> {
const { name } = this.state;
const idp: IdentityProvider = {
name,
Expand All @@ -56,7 +56,7 @@ export class AddHTPasswdPage extends PromiseComponent<{}, AddHTPasswdPageState>
},
};

return this.handlePromise(addIDP(oauth, idp));
return this.handlePromise(addIDP(oauth, idp, dryRun));
}

submit: React.FormEventHandler<HTMLFormElement> = (e) => {
Expand All @@ -69,9 +69,15 @@ export class AddHTPasswdPage extends PromiseComponent<{}, AddHTPasswdPageState>
// Clear any previous errors.
this.setState({ errorMessage: '' });
this.getOAuthResource().then((oauth: OAuthKind) => {
return this.createHTPasswdSecret()
.then((secret: K8sResourceKind) => this.addHTPasswdIDP(oauth, secret.metadata.name))
.then(redirectToOAuthPage);
this.addHTPasswdIDP(oauth, mockNames.secret, true)
.then(() => {
return this.createHTPasswdSecret()
.then((secret: K8sResourceKind) => this.addHTPasswdIDP(oauth, secret.metadata.name))
.then(redirectToOAuthPage);
})
.catch((err) => {
this.setState({ errorMessage: err });
});
});
};

Expand Down
14 changes: 12 additions & 2 deletions frontend/public/components/cluster-settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,30 @@ import * as _ from 'lodash-es';
import { OAuthModel } from '../../models';
import { IdentityProvider, k8sGet, k8sPatch, OAuthKind } from '../../module/k8s';
import { history, resourcePathFromModel } from '../utils';
import { dryRunOpt } from '@console/dev-console/src/utils/shared-submit-utils';

// The name of the cluster-scoped OAuth configuration resource.
const OAUTH_RESOURCE_NAME = 'cluster';

export const getOAuthResource = (): Promise<OAuthKind> => k8sGet(OAuthModel, OAUTH_RESOURCE_NAME);

export const addIDP = (oauth: OAuthKind, idp: IdentityProvider): Promise<OAuthKind> => {
export const addIDP = (
oauth: OAuthKind,
idp: IdentityProvider,
dryRun?: boolean,
): Promise<OAuthKind> => {
const patch = _.isEmpty(oauth.spec.identityProviders)
? { op: 'add', path: '/spec/identityProviders', value: [idp] }
: { op: 'add', path: '/spec/identityProviders/-', value: idp };
return k8sPatch(OAuthModel, oauth, [patch]);
return k8sPatch(OAuthModel, oauth, [patch], dryRun ? dryRunOpt : {});
};

export const redirectToOAuthPage = () => {
const path = resourcePathFromModel(OAuthModel, OAUTH_RESOURCE_NAME);
history.push(path);
};

export const mockNames = {
secret: 'secret-name',
ca: 'ca-name',
};
31 changes: 21 additions & 10 deletions frontend/public/components/cluster-settings/keystone-idp-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ActionGroup, Button } from '@patternfly/react-core';
import { SecretModel, ConfigMapModel } from '../../models';
import { IdentityProvider, k8sCreate, K8sResourceKind, OAuthKind } from '../../module/k8s';
import { ButtonBar, PromiseComponent, history, AsyncComponent } from '../utils';
import { addIDP, getOAuthResource, redirectToOAuthPage } from './';
import { addIDP, getOAuthResource, redirectToOAuthPage, mockNames } from './';
import { IDPNameInput } from './idp-name-input';
import { IDPCAFileInput } from './idp-cafile-input';

Expand Down Expand Up @@ -76,7 +76,12 @@ export class AddKeystonePage extends PromiseComponent<{}, AddKeystonePageState>
return this.handlePromise(k8sCreate(ConfigMapModel, ca));
}

addKeystoneIDP(oauth: OAuthKind, secretName: string, caName: string): Promise<K8sResourceKind> {
addKeystoneIDP(
oauth: OAuthKind,
secretName: string,
caName: string,
dryRun?: boolean,
): Promise<K8sResourceKind> {
const { name, domainName, url } = this.state;
const idp: IdentityProvider = {
name,
Expand All @@ -103,7 +108,7 @@ export class AddKeystonePage extends PromiseComponent<{}, AddKeystonePageState>
};
}

return this.handlePromise(addIDP(oauth, idp));
return this.handlePromise(addIDP(oauth, idp, dryRun));
}

submit: React.FormEventHandler<HTMLFormElement> = (e) => {
Expand All @@ -117,15 +122,21 @@ export class AddKeystonePage extends PromiseComponent<{}, AddKeystonePageState>
// Clear any previous errors.
this.setState({ errorMessage: '' });
this.getOAuthResource().then((oauth: OAuthKind) => {
const promises = [this.createTLSSecret(), this.createCAConfigMap()];
this.addKeystoneIDP(oauth, mockNames.secret, mockNames.ca, true)
.then(() => {
const promises = [this.createTLSSecret(), this.createCAConfigMap()];

Promise.all(promises)
.then(([tlsSecret, configMap]) => {
const caName = configMap ? configMap.metadata.name : '';
const secretName = tlsSecret ? tlsSecret.metadata.name : '';
return this.addKeystoneIDP(oauth, secretName, caName);
Promise.all(promises)
.then(([tlsSecret, configMap]) => {
const caName = configMap ? configMap.metadata.name : '';
const secretName = tlsSecret ? tlsSecret.metadata.name : '';
return this.addKeystoneIDP(oauth, secretName, caName);
})
.then(redirectToOAuthPage);
})
.then(redirectToOAuthPage);
.catch((err) => {
this.setState({ errorMessage: err });
});
});
};

Expand Down
Loading

0 comments on commit 07f558e

Please sign in to comment.