Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for basic authentication #15

Merged
merged 2 commits into from
Jan 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions electron/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ server.post('/request', function (req, res) {
options.headers['Authorization'] = 'Bearer ' + postData.token;
}

if (postData.username !== '' && postData.password !== '') {
options.headers['Authorization'] = 'Basic ' + Buffer.from(postData.username + ':' + postData.password).toString('base64');
}

const request = https.request(postData.url, options, function(response) {
let body = '';

Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@ionic/react": "^4.11.7",
"@ionic/react-hooks": "0.0.5",
"@ionic/react-router": "^4.11.7",
"@kubenav/kubenav-plugin": "1.1.3",
"@kubenav/kubenav-plugin": "1.2.0",
"@kubernetes/client-node": "^0.11.0",
"@types/jest": "^24.0.24",
"@types/node": "^12.12.21",
Expand Down
30 changes: 27 additions & 3 deletions src/components/settings/AddCluster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ const AddCluster: React.FunctionComponent = () => {
const [clientKeyData, setClientKeyData] = useState<string>('');
const [kubeconfig, setKubeconfig] = useState<string>('');
const [token, setToken] = useState<string>('');
const [username, setUsername] = useState<string>('');
const [password, setPassword] = useState<string>('');

const handleType = (event) => {
setType(event.detail.value);
Expand Down Expand Up @@ -87,6 +89,14 @@ const AddCluster: React.FunctionComponent = () => {
setToken(event.target.value);
};

const handleUsername = (event) => {
setUsername(event.target.value);
};

const handlePassword = (event) => {
setPassword(event.target.value);
};

const handleKubeconfig = (event) => {
setKubeconfig(event.target.value);
};
Expand All @@ -100,8 +110,8 @@ const AddCluster: React.FunctionComponent = () => {
setError('Invalid URL')
} else if (type === 'manual' && certificateAuthorityData === '') {
setError('Certificate Authority Data is required')
} else if (type === 'manual' && clientCertificateData === '' && clientKeyData === '' && token === '') {
setError('Client Certificate Data and Client Key Data or Token is required')
} else if (type === 'manual' && clientCertificateData === '' && clientKeyData === '' && token === '' && username === '' && password === '') {
setError('Client Certificate Data and Client Key Data or Token or Username and Password is required')
} else if (type === 'kubeconfig' && kubeconfig === '') {
setError('Kubeconfig is required')
} else {
Expand All @@ -115,6 +125,8 @@ const AddCluster: React.FunctionComponent = () => {
clientCertificateData: clientCertificateData,
clientKeyData: clientKeyData,
token: token,
username: username,
password: password,
namespace: 'default',
}]);
} else if (type === 'kubeconfig') {
Expand All @@ -125,7 +137,7 @@ const AddCluster: React.FunctionComponent = () => {
const cluster = getKubeconfigCluster(ctx.context.cluster, config.clusters);
const user = getKubeconfigUser(ctx.context.user, config.users);

if (ctx.name === '' || cluster === null || user === null || !cluster.server || !cluster['certificate-authority-data'] || !((user['client-certificate-data'] && user['client-key-data']) || user.token)) {
if (ctx.name === '' || cluster === null || user === null || !cluster.server || !cluster['certificate-authority-data'] || !((user['client-certificate-data'] && user['client-key-data']) || user.token || !(user.username && user.password))) {
throw new Error('Invalid kubeconfig');
}

Expand All @@ -137,6 +149,8 @@ const AddCluster: React.FunctionComponent = () => {
clientCertificateData: user['client-certificate-data'] ? user['client-certificate-data'] : '',
clientKeyData: user['client-key-data'] ? user['client-key-data'] : '',
token: user.token ? user.token : '',
username: user.username ? user.username : '',
password: user.password ? user.password : '',
namespace: 'default',
});
}
Expand All @@ -151,6 +165,8 @@ const AddCluster: React.FunctionComponent = () => {
setClientCertificateData('');
setClientKeyData('');
setToken('');
setUsername('');
setPassword('');
setShowModal(false);
} catch (err) {
setError(err.message);
Expand Down Expand Up @@ -225,6 +241,14 @@ const AddCluster: React.FunctionComponent = () => {
<IonLabel position="stacked">Token</IonLabel>
<IonTextarea autoGrow={true} value={token} onInput={handleToken} />
</IonItem>
<IonItem>
<IonLabel position="stacked">Username</IonLabel>
<IonInput type="text" value={username} onInput={handleUsername} />
</IonItem>
<IonItem>
<IonLabel position="stacked">Password</IonLabel>
<IonInput type="password" value={password} onInput={handlePassword} />
</IonItem>
</IonList>
) : null}

Expand Down
24 changes: 22 additions & 2 deletions src/components/settings/EditCluster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const EditCluster: React.FunctionComponent<IEditClusterProps> = ({ cluster }) =>
const [clientCertificateData, setClientCertificateData] = useState<string>(cluster.clientCertificateData);
const [clientKeyData, setClientKeyData] = useState<string>(cluster.clientKeyData);
const [token, setToken] = useState<string>(cluster.token);
const [username, setUsername] = useState<string>(cluster.username);
const [password, setPassword] = useState<string>(cluster.password);

const handleName = (event) => {
setName(event.target.value);
Expand All @@ -61,6 +63,14 @@ const EditCluster: React.FunctionComponent<IEditClusterProps> = ({ cluster }) =>
setToken(event.target.value);
};

const handleUsername = (event) => {
setUsername(event.target.value);
};

const handlePassword = (event) => {
setPassword(event.target.value);
};

const editCluster = () => {
if (name === '') {
setError('Name is required')
Expand All @@ -70,8 +80,8 @@ const EditCluster: React.FunctionComponent<IEditClusterProps> = ({ cluster }) =>
setError('Invalid URL')
} else if (certificateAuthorityData === '') {
setError('Certificate Authority Data is required')
} else if (clientCertificateData === '' && clientKeyData === '' && token === '') {
setError('Client Certificate Data and Client Key Data or Token is required')
} else if (clientCertificateData === '' && clientKeyData === '' && token === '' && username === '' && password === '') {
setError('Client Certificate Data and Client Key Data or Token or Username and Password is required')
} else {
context.editCluster({
id: cluster.id,
Expand All @@ -81,6 +91,8 @@ const EditCluster: React.FunctionComponent<IEditClusterProps> = ({ cluster }) =>
clientCertificateData: clientCertificateData,
clientKeyData: clientKeyData,
token: token,
username: username,
password: password,
namespace: cluster.namespace,
});

Expand Down Expand Up @@ -140,6 +152,14 @@ const EditCluster: React.FunctionComponent<IEditClusterProps> = ({ cluster }) =>
<IonLabel position="stacked">Token</IonLabel>
<IonTextarea autoGrow={true} value={token} onInput={handleToken} />
</IonItem>
<IonItem>
<IonLabel position="stacked">Username</IonLabel>
<IonInput type="text" value={username} onInput={handleUsername} />
</IonItem>
<IonItem>
<IonLabel position="stacked">Password</IonLabel>
<IonInput type="password" value={password} onInput={handlePassword} />
</IonItem>
</IonList>
</IonContent>
</IonModal>
Expand Down
12 changes: 7 additions & 5 deletions src/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,14 @@ export const AppContextProvider: React.FunctionComponent = ({ children }) => {
let data = await plugin.request({
server: SERVER,
method: method,
url: alternativeCluster ? alternativeCluster.url : clusters![cluster!].url + url,
url: alternativeCluster ? alternativeCluster.url : clusters && cluster && clusters[cluster].url ? clusters[cluster].url + url : '',
body: body,
certificateAuthorityData: alternativeCluster ? alternativeCluster.certificateAuthorityData : clusters![cluster!].certificateAuthorityData,
clientCertificateData: alternativeCluster ? alternativeCluster.clientCertificateData : clusters![cluster!].clientCertificateData,
clientKeyData: alternativeCluster ? alternativeCluster.clientKeyData : clusters![cluster!].clientKeyData,
token: alternativeCluster ? alternativeCluster.token : clusters![cluster!].token,
certificateAuthorityData: alternativeCluster ? alternativeCluster.certificateAuthorityData : clusters && cluster && clusters[cluster].certificateAuthorityData ? clusters[cluster].certificateAuthorityData : '',
clientCertificateData: alternativeCluster ? alternativeCluster.clientCertificateData : clusters && cluster && clusters[cluster].clientCertificateData ? clusters[cluster].clientCertificateData : '',
clientKeyData: alternativeCluster ? alternativeCluster.clientKeyData : clusters && cluster && clusters[cluster].clientKeyData ? clusters[cluster].clientKeyData : '',
token: alternativeCluster ? alternativeCluster.token : clusters && cluster && clusters[cluster].token ? clusters[cluster].token : '',
username: alternativeCluster ? alternativeCluster.username : clusters && cluster && clusters[cluster].username ? clusters[cluster].username : '',
password: alternativeCluster ? alternativeCluster.password : clusters && cluster && clusters[cluster].password ? clusters[cluster].password : '',
});

if (isJSON(data.data)) {
Expand Down
6 changes: 5 additions & 1 deletion src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export interface ICluster {
clientCertificateData: string;
clientKeyData: string;
token: string;
username: string;
password: string;
namespace: string;
}

Expand Down Expand Up @@ -96,7 +98,9 @@ export interface IKubeconfigContext {
export interface IKubeconfigUser {
'client-certificate-data'?: string;
'client-key-data'?: string;
token?: string
token?: string;
username?: string;
password?: string;
}

export interface IKubeconfigUserRef {
Expand Down