-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.tsx
222 lines (211 loc) · 7.63 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import { MenuItem } from '@material-ui/core';
import { Flex, GitRepository, Link } from '@weaveworks/weave-gitops';
import { useCallback, useMemo, useState } from 'react';
import { GitProvider } from '../../../api/gitauth/gitauth.pb';
import { useEnterpriseClient } from '../../../contexts/API';
import CallbackStateContextProvider from '../../../contexts/GitAuth/CallbackStateContext';
import useNotifications from '../../../contexts/Notifications';
import {
expiredTokenNotification,
useIsAuthenticated,
} from '../../../hooks/gitprovider';
import { useCallbackState } from '../../../utils/callback-state';
import { InputDebounced, Select, validateFormData } from '../../../utils/form';
import { Routes } from '../../../utils/nav';
import { removeToken } from '../../../utils/request';
import { clearCallbackState, getProviderToken } from '../../GitAuth/utils';
import { Page } from '../../Layout/App';
import { NotificationsWrapper } from '../../Layout/NotificationsWrapper';
import GitOps from '../../Templates/Form/Partials/GitOps';
import { getRepositoryUrl } from '../../Templates/Form/utils';
import ListClusters from '../Shared/ListClusters';
import ListKustomizations from '../Shared/ListKustomizations';
import { Preview } from '../Shared/Preview';
import {
getFormattedPayload,
scrollToAlertSection,
handleError,
getInitialData,
SOPS,
FormWrapperSecret,
} from '../Shared/utils';
import SecretData from './SecretData';
const CreateSOPS = () => {
const callbackState = useCallbackState();
const random = useMemo(() => Math.random().toString(36).substring(7), []);
const { initialFormData } = getInitialData(callbackState, random);
const [showAuthDialog, setShowAuthDialog] = useState(false);
const [formError, setFormError] = useState<string>('');
const [formData, setFormData] = useState<SOPS>(initialFormData);
const handleFormData = (value: any, key: string) => {
setFormData(f => ({ ...f, [key]: value }));
};
const { setNotifications } = useNotifications();
const [loading, setLoading] = useState<boolean>(false);
const token = getProviderToken(formData.provider as GitProvider);
const { isAuthenticated, validateToken } = useIsAuthenticated(
formData.provider as GitProvider,
token,
);
const { clustersService } = useEnterpriseClient();
const handleCreateSecret = useCallback(() => {
setLoading(true);
validateToken()
.then(async () => {
try {
const { encryptionPayload, cluster } = getFormattedPayload(formData);
const encrypted = await clustersService.EncryptSopsSecret(
encryptionPayload,
);
const response = await clustersService.CreateAutomationsPullRequest(
{
headBranch: formData.branchName,
title: formData.pullRequestTitle,
description: formData.pullRequestDescription,
commitMessage: formData.commitMessage,
repositoryUrl: getRepositoryUrl(formData.repo as GitRepository),
clusterAutomations: [
{
cluster,
isControlPlane: cluster.namespace ? true : false,
sopsSecret: {
...encrypted.encryptedSecret,
},
filePath: encrypted.path,
},
],
},
{
headers: new Headers({ 'Git-Provider-Token': `token ${token}` }),
},
);
setNotifications([
{
message: {
component: (
<Link href={response.webUrl} newTab>
PR created successfully, please review and merge the pull
request to apply the changes to the cluster.
</Link>
),
},
severity: 'success',
},
]);
scrollToAlertSection();
} catch (error: any) {
handleError(error, setNotifications);
} finally {
setLoading(false);
removeToken(formData.provider);
clearCallbackState();
}
})
.catch(() => {
removeToken(formData.provider);
setNotifications([expiredTokenNotification]);
})
.finally(() => setLoading(false));
}, [clustersService, formData, setNotifications, token, validateToken]);
const authRedirectPage = Routes.CreateSopsSecret;
return (
<Page
path={[
{ label: 'Secrets', url: Routes.Secrets },
{ label: 'Create new sops secret' },
]}
>
<CallbackStateContextProvider
callbackState={{
page: authRedirectPage,
state: {
formData,
},
}}
>
<NotificationsWrapper>
<FormWrapperSecret
noValidate
onSubmit={event =>
validateFormData(event, handleCreateSecret, setFormError)
}
>
<Flex column>
<ListClusters
value={formData.clusterName}
handleFormData={(val: any) => {
handleFormData(val, 'clusterName');
handleFormData('', 'kustomization');
}}
hasError={formError === 'clusterName' && !formData.clusterName}
/>
<InputDebounced
required
name="secretName"
label="SECRET NAME"
value={formData.secretName}
handleFormData={val => handleFormData(val, 'secretName')}
formError={formError}
/>
<InputDebounced
required
name="secretNamespace"
label="SECRET NAMESPACE"
value={formData.secretNamespace}
handleFormData={val => handleFormData(val, 'secretNamespace')}
formError={formError}
/>
</Flex>
<h2>Encryption</h2>
<Select
required
name="encryptionType"
label="ENCRYPT USING"
value={formData.encryptionType}
onChange={event =>
handleFormData(event.target.value, 'encryptionType')
}
>
<MenuItem value="GPG/AGE">GPG / AGE</MenuItem>
</Select>
{!!formData.clusterName && (
<ListKustomizations
value={formData.kustomization}
handleFormData={(val: any) =>
handleFormData(val, 'kustomization')
}
clusterName={formData.clusterName}
hasError={
formError === 'kustomization' && !formData.kustomization
}
/>
)}
<h2>Secret Data</h2>
<p className="secret-data-hint">
Please note that we will encode the secret values to base64 before
encryption
</p>
<SecretData
formData={formData}
setFormData={setFormData}
formError={formError}
/>
<GitOps
loading={loading}
isAuthenticated={isAuthenticated}
formData={formData}
setFormData={setFormData}
showAuthDialog={showAuthDialog}
setShowAuthDialog={setShowAuthDialog}
formError={formError}
enableGitRepoSelection={true}
>
<Preview formData={formData} setFormError={setFormError} />
</GitOps>
</FormWrapperSecret>
</NotificationsWrapper>
</CallbackStateContextProvider>
</Page>
);
};
export default CreateSOPS;