Skip to content

Commit

Permalink
Merge pull request #63 from sighupio/bug/fix-template
Browse files Browse the repository at this point in the history
fixes templateRoles not populated correctly
  • Loading branch information
Rampage1xx authored Feb 24, 2021
2 parents d720ea5 + 634f897 commit 59d1720
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 21 deletions.
6 changes: 3 additions & 3 deletions web-client/src/components/Summary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ export default function Summary({ pairItems }: SummaryParameters) {

const ruleSets: RuleSet[] = pairItems
.reduce((acc, p) => {
const crs = clusterRoles.filter(c => c.metadata.name === p.roleName)
const crs = clusterRoles.filter(c => c.metadata.name === p.template)

const rules = crs.reduce((acc, v) => {
acc = acc.concat(v.rules)
return acc
}, [])

const ruleset = {
template: p.roleName,
template: p.template,
rules,
namespaces: p.namespaces === 'ALL_NAMESPACES' ? ['all'] : p.namespaces
}

const itemForSameTemplate = acc.find(rs => rs.template === p.roleName)
const itemForSameTemplate = acc.find(rs => rs.template === p.template)
if (itemForSameTemplate) {
if (
itemForSameTemplate.namespaces.includes('all') ||
Expand Down
6 changes: 3 additions & 3 deletions web-client/src/components/TemplatePairSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface TemplatePairSelectParameters {
export default function TemplatePairSelect({onSave, initialValues}: TemplatePairSelectParameters) {
const [namespaces, setNamespaces] = useState<AggregatedRoleBindingNamespace>(initialValues.namespaces || [])
const [allNamespace, setAllNamespaces] = useState<boolean>(initialValues.namespaces === 'ALL_NAMESPACES' ? true : null)
const [template, setTemplate] = useState(initialValues.roleName)
const [template, setTemplate] = useState(initialValues.template)

useEffect(() => {
if (allNamespace === null) {
Expand All @@ -33,7 +33,7 @@ export default function TemplatePairSelect({onSave, initialValues}: TemplatePair
onSave({
id: initialValues.id,
namespaces,
roleName: template
template: template
})
}, [initialValues.id, namespaces, onSave, template])

Expand All @@ -45,7 +45,7 @@ export default function TemplatePairSelect({onSave, initialValues}: TemplatePair
</div>
<TemplateSelect
onSelect={t => setTemplate(t)}
initialValue={initialValues.roleName}
initialValue={initialValues.template}
/>
</div>
<div style={{marginLeft: 20, flex: 3}} data-testid="namespaces-select">
Expand Down
2 changes: 1 addition & 1 deletion web-client/src/components/Templates.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface TemplatesParameters extends AggregatedRoleBindingManager {
export default function Templates({pairItems, savePair, setPairItems, addEmptyPair}: TemplatesParameters) {
const lastPair = pairItems[pairItems.length - 1]
const addButtonDisabled =
lastPair && lastPair.roleName && lastPair.namespaces.length === 0
lastPair && lastPair.template && lastPair.namespaces.length === 0

return (
<div>
Expand Down
2 changes: 1 addition & 1 deletion web-client/src/components/edit-user.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export default function EditUser({user}: EditUserParameters) {
}, [])

const addEmptyPair = useCallback(() => {
setAggregatedRoleBindings(state => [...state, {id: uuid.v4(), namespaces: [], roleName: ''}])
setAggregatedRoleBindings(state => [...state, {id: uuid.v4(), namespaces: [], template: ''}])
}, [])

const saveButtonDisabled = aggregatedRoleBindings.length === 0 || aggregatedRoleBindings.some(p => p.namespaces.length === 0)
Expand Down
2 changes: 1 addition & 1 deletion web-client/src/components/new-user-wizard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export default function NewUserWizard() {
}, [])

const addEmptyPair = useCallback(() => {
setAggregatedRoleBindings(state => [...state, {id: uuid.v4(), namespaces: [], roleName: ''}])
setAggregatedRoleBindings(state => [...state, {id: uuid.v4(), namespaces: [], template: ''}])
}, [])

useEffect(addEmptyPair, [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function NewClusterRoleBindingForm({fetchData}) {
...s,
namespace: 'permission-manager'
})),
roleName,
template: roleName,
clusterRolebindingName,
})

Expand Down
2 changes: 1 addition & 1 deletion web-client/src/deadcode/components/rolebindings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function NewRoleBindingForm({fetchData}: { fetchData(): void }) {

await httpRequests.rolebindingRequests.create.rolebinding({
namespace,
roleName,
template: roleName,
roleBindingName: rolebindingName,
roleKind,
subjects: subjects.map(s => ({
Expand Down
3 changes: 2 additions & 1 deletion web-client/src/services/role.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ export interface AggregatedRoleBinding {

readonly namespaces: AggregatedRoleBindingNamespace
/**
* DO NOT REFACTOR THIS NAME
* example: template-namespaced-resources___developer
*/
readonly roleName: string
readonly template: string
}


Expand Down
19 changes: 10 additions & 9 deletions web-client/src/services/rolebindingCreateRequests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import {Subject} from "../hooks/useRbac";

interface RoleCreate {
/**
* DO NOT REFACTOR THIS NAME
* rolename
*/
readonly roleName: string,
readonly template: string,
/**
* this field adds generated_for_user to the http request
*/
Expand Down Expand Up @@ -61,7 +62,7 @@ export class RolebindingCreateRequests {
*/
private async httpCreateClusterRolebinding(params: ClusterRolebindingCreate) {
const request = {
roleName: params.roleName,
roleName: params.template,
subjects: params.subjects,
clusterRolebindingName: params.clusterRolebindingName
};
Expand All @@ -83,7 +84,7 @@ export class RolebindingCreateRequests {

return await this.httpCreateClusterRolebinding({
clusterRolebindingName: params.clusterRolebindingName,
roleName: params.roleName,
template: params.template,
username: params.username,
subjects: params.subjects
})
Expand All @@ -96,7 +97,7 @@ export class RolebindingCreateRequests {
*/
public async rolebinding(params: RolebindingCreate) {
const request = {
roleName: params.roleName,
roleName: params.template,
namespace: params.namespace,
roleKind: params.roleKind,
subjects: params.subjects,
Expand Down Expand Up @@ -144,15 +145,15 @@ export class RolebindingCreateRequests {
// we grab all the 'ALL_NAMESPACE' rolebindings and create them on the backend
for (const allNamespaceRolebinding of aggregatedRoleBindings.filter(e => e.namespaces === 'ALL_NAMESPACES')) {
// we construct the resource name
const clusterRolebindingName = username + resourceSeparator + allNamespaceRolebinding.roleName + 'all_namespaces'
const clusterRolebindingName = username + resourceSeparator + allNamespaceRolebinding.template + 'all_namespaces'

// means that we already created the resource
if (consumed.includes(clusterRolebindingName)) continue;

await this.rolebindingAllNamespaces({
clusterRolebindingName: clusterRolebindingName,
// addGeneratedForUser: false,
roleName: allNamespaceRolebinding.roleName,
template: allNamespaceRolebinding.template,
// username: params.username,
subjects: subjects
})
Expand All @@ -165,13 +166,13 @@ export class RolebindingCreateRequests {
for (const namespace of namespacedRoleBinding.namespaces) {

// we construct the resource name
const rolebindingName = username + resourceSeparator + namespacedRoleBinding.roleName + resourceSeparator + namespace
const rolebindingName = username + resourceSeparator + namespacedRoleBinding.template + resourceSeparator + namespace

// means that we already created the resource
if (consumed.includes(rolebindingName)) continue;

await this.rolebinding({
roleName: namespacedRoleBinding.roleName,
template: namespacedRoleBinding.template,
username: username,
namespace: namespace,
roleBindingName: rolebindingName,
Expand All @@ -197,7 +198,7 @@ export class RolebindingCreateRequests {

//todo this must be changed in the future to support dynamic cluster roles. Right now it's just a single api call based on a radio select
await this.clusterRolebinding({
roleName: roleName,
template: roleName,
clusterRolebindingName: clusterRolebindingName,
// username: params.username,
// addGeneratedForUser: false,
Expand Down

0 comments on commit 59d1720

Please sign in to comment.