Skip to content

Commit

Permalink
Merge pull request #40 from InfuseAI/feature/sc-23417/crane-check-set…
Browse files Browse the repository at this point in the history
…tings-on-save-and-show-results

Feature/sc 23417/crane test settings button for dockerhub and aws
  • Loading branch information
aar0nTw authored Dec 9, 2021
2 parents 6b1d83f + 24246cc commit 2ffee54
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 6 deletions.
64 changes: 59 additions & 5 deletions src/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function Settings() {
account: values['docker-account'],
password: values['docker-password'],
});
notification.info({
notification.success({
message: 'DockerHub setup saved.',
});
};
Expand All @@ -46,7 +46,7 @@ export default function Settings() {
secretKey: values['aws-key'],
region: values['aws-region'],
});
notification.info({
notification.success({
message: 'AWS setup saved.',
});
};
Expand All @@ -55,7 +55,7 @@ export default function Settings() {
endpoint: values['primehub-api-endpoint'],
token: values['primehub-api-token'],
});
notification.info({
notification.success({
message: 'PrimeHub setup saved.',
});
};
Expand Down Expand Up @@ -100,6 +100,48 @@ export default function Settings() {
});
}
};
const onAWSTest = async () => {
const accessKeyId = awsForm.getFieldValue('aws-id');
const secretAccessKey = awsForm.getFieldValue('aws-key');
const region = awsForm.getFieldValue('aws-region');
const result = await send('test-aws-credentials', {
accessKeyId,
secretAccessKey,
region,
});
if (result.error) {
console.log(result.error);
notification.error({
message: 'AWS Credential Check Failed',
description: ``,
});
} else {
notification.success({
message: 'AWS Connected',
description: ``,
});
}
};
const onDockerHubTest = async () => {
const username = dockerHubForm.getFieldValue('docker-account');
const password = dockerHubForm.getFieldValue('docker-password');
const result = await send('test-dockerhub-credentials', {
username,
password,
});
if (!result) {
console.log(result.error);
notification.error({
message: 'DockerHub Credential Check Failed',
description: ``,
});
} else {
notification.success({
message: 'DockerHub Connected',
description: ``,
});
}
};
const onDockerHubReset = async () => {
await send('delete-dockerhub-credential');
notification.info({
Expand Down Expand Up @@ -207,7 +249,13 @@ export default function Settings() {
<Input />
</Form.Item>
<Form.Item label='Password' name='docker-password'>
<Input.Password />
<Input.Password
addonAfter={
<Button type='text' onClick={onDockerHubTest}>
Test
</Button>
}
/>
</Form.Item>
<Form.Item style={{ textAlign: 'right' }}>
<Button type='primary' htmlType='submit'>
Expand Down Expand Up @@ -271,7 +319,13 @@ export default function Settings() {
<Input />
</Form.Item>
<Form.Item label='Secret Access Key' name='aws-key'>
<Input.Password />
<Input.Password
addonAfter={
<Button type='text' onClick={onAWSTest}>
Test
</Button>
}
/>
</Form.Item>
<Form.Item style={{ textAlign: 'right' }}>
<Button type='primary' htmlType='submit'>
Expand Down
15 changes: 15 additions & 0 deletions src/electron/AwsAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as AWS from 'aws-sdk';

interface AwsAdapterConfig {
accessKey: string;
secretKey: string;
Expand Down Expand Up @@ -42,6 +43,20 @@ export default class AwsAdapter {
return AwsAdapter.instance;
}

public static async verifyCredentials(
accessKeyId: string,
secretAccessKey: string,
region: string
): Promise<AWS.STS.GetCallerIdentityResponse> {
return await new AWS.STS({
accessKeyId,
secretAccessKey,
region,
})
.getCallerIdentity()
.promise();
}

public async verifyAccessPermission(): Promise<AWS.STS.GetCallerIdentityResponse> {
return await this.sts.getCallerIdentity().promise();
}
Expand Down
13 changes: 13 additions & 0 deletions src/electron/DockerHubAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ export default class DockerHubAdapter {
return DockerHubAdapter.instance;
}

public static async verifyCredentials(
username: string,
password: string
): Promise<boolean> {
const loginUrl = `${API_URL}/users/login`;
const response = await axios.post(loginUrl, { username, password });
const token = get(response, 'data.token', null);
if (token) {
return true;
}
return false;
}

public async login(): Promise<void> {
const loginUrl = `${API_URL}/users/login`;
const response = await axios.post(loginUrl, this.credential);
Expand Down
23 changes: 22 additions & 1 deletion src/electron/ServerHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Sentry.init({
dsn: 'https://[email protected]/6088888',
});


const docker = new Docker();
const dockerHubCredentialKeyName = 'Crane-DockerHub';
const primeHubCredentialKeyName = 'Crane-PrimeHub';
Expand Down Expand Up @@ -180,6 +179,28 @@ const handlers = {
console.log(error);
}
},
'test-aws-credentials': async (args) => {
try {
return await AwsAdapter.verifyCredentials(
args.accessKeyId,
args.secretAccessKey,
args.region
);
} catch (error) {
return { error };
}
},
'test-dockerhub-credentials': async (args) => {
try {
return await DockerHubAdapter.verifyCredentials(
args.username,
args.password
);
} catch (error) {
console.log(error);
return false;
}
},
'save-primehub-credential': async (args) =>
await saveCredential(primeHubCredentialKeyName, args.endpoint, args.token),
'save-aws-credential': async (args) => {
Expand Down

0 comments on commit 2ffee54

Please sign in to comment.