Skip to content

Commit 8d7cddc

Browse files
authored
Fix create bucket and list bucket for wildcard statements in policies (#1589)
Signed-off-by: Daniel Valdivia <[email protected]>
1 parent 35f9743 commit 8d7cddc

File tree

4 files changed

+68
-30
lines changed

4 files changed

+68
-30
lines changed

portal-ui/src/common/SecureComponent/__tests__/accessControl.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,25 @@ const setPolicy2 = () => {
9696
},
9797
});
9898
};
99+
const setPolicy3 = () => {
100+
store.dispatch({
101+
type: SESSION_RESPONSE,
102+
message: {
103+
distributedMode: true,
104+
features: [],
105+
permissions: {
106+
"arn:aws:s3:::testbucket-*": [
107+
"admin:CreateServiceAccount",
108+
"s3:*",
109+
"admin:CreateUser",
110+
],
111+
"console-ui": ["admin:CreateServiceAccount", "admin:CreateUser"],
112+
},
113+
status: "ok",
114+
operator: false,
115+
},
116+
});
117+
};
99118

100119
test("Upload button disabled", () => {
101120
setPolicy1();
@@ -123,3 +142,18 @@ test("Can List Objects In Bucket", () => {
123142
setPolicy2();
124143
expect(hasPermission("bucket-svc", [IAM_SCOPES.S3_LIST_BUCKET])).toBe(true);
125144
});
145+
146+
test("Can create bucket for policy with a wildcard", () => {
147+
setPolicy3();
148+
expect(hasPermission("*", [IAM_SCOPES.S3_CREATE_BUCKET])).toBe(true);
149+
});
150+
151+
test("Can browse a bucket for a policy with a wildcard", () => {
152+
setPolicy3();
153+
expect(
154+
hasPermission(
155+
"testbucket-0",
156+
IAM_PAGES_PERMISSIONS[IAM_PAGES.BUCKETS_BROWSE_VIEW]
157+
)
158+
).toBe(true);
159+
});

portal-ui/src/common/SecureComponent/accessControl.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const hasPermission = (
5353

5454
const replaceWildcard = wildcardItemSection
5555
.replace("/", "\\/")
56-
.replace("\\/*", "($|(\\/.*?))");
56+
.replace("*", "($|\\/?(.*?))");
5757

5858
const inRegExp = new RegExp(`${replaceWildcard}$`, "gm");
5959

@@ -105,8 +105,26 @@ const hasPermission = (
105105
});
106106
}
107107

108+
let anyResourceGrant: string[] = [];
109+
if (resource === "*") {
110+
Object.entries(sessionGrants).forEach(([key, values]) => {
111+
scopes.forEach((scope) => {
112+
values.forEach((val) => {
113+
if (val === scope || val === "s3:*") {
114+
anyResourceGrant = [...anyResourceGrant, scope];
115+
}
116+
});
117+
});
118+
});
119+
}
120+
108121
return hasAccessToResource(
109-
[...resourceGrants, ...globalGrants, ...containsResourceGrants],
122+
[
123+
...resourceGrants,
124+
...globalGrants,
125+
...containsResourceGrants,
126+
...anyResourceGrant,
127+
],
110128
scopes,
111129
matchAll
112130
);

portal-ui/src/screens/Console/Buckets/ListBuckets/ListBuckets.tsx

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import SearchBox from "../../Common/SearchBox";
5151
import VirtualizedList from "../../Common/VirtualizedList/VirtualizedList";
5252
import RBIconButton from "../BucketDetails/SummaryItems/RBIconButton";
5353
import BulkLifecycleModal from "./BulkLifecycleModal";
54+
import hasPermission from "../../../../common/SecureComponent/accessControl";
5455

5556
const styles = (theme: Theme) =>
5657
createStyles({
@@ -200,10 +201,7 @@ const ListBuckets = ({
200201
return null;
201202
};
202203

203-
const createBucketButtonResources: string[] =
204-
session && session.permissions
205-
? Array.from(Object.keys(session.permissions)) || []
206-
: [];
204+
const canCreateBucket = hasPermission("*", [IAM_SCOPES.S3_CREATE_BUCKET]);
207205

208206
return (
209207
<Fragment>
@@ -293,22 +291,17 @@ const ListBuckets = ({
293291
variant={"outlined"}
294292
/>
295293

296-
<SecureComponent
297-
scopes={[IAM_SCOPES.S3_CREATE_BUCKET]}
298-
resource={createBucketButtonResources}
299-
errorProps={{ disabled: true }}
300-
>
301-
<RBIconButton
302-
tooltip={"Create Bucket"}
303-
onClick={() => {
304-
history.push("/add-bucket");
305-
}}
306-
text={"Create Bucket"}
307-
icon={<AddIcon />}
308-
color={"primary"}
309-
variant={"contained"}
310-
/>
311-
</SecureComponent>
294+
<RBIconButton
295+
tooltip={"Create Bucket"}
296+
onClick={() => {
297+
history.push("/add-bucket");
298+
}}
299+
text={"Create Bucket"}
300+
icon={<AddIcon />}
301+
color={"primary"}
302+
variant={"contained"}
303+
disabled={!canCreateBucket}
304+
/>
312305
</Grid>
313306
</Grid>
314307

portal-ui/src/screens/Console/Console.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,7 @@ const Console = ({
219219
component: Buckets,
220220
path: IAM_PAGES.ADD_BUCKETS,
221221
customPermissionFnc: () => {
222-
const createBucketResources: string[] =
223-
session && session.permissions
224-
? Array.from(Object.keys(session.permissions)) || []
225-
: [];
226-
return hasPermission(
227-
createBucketResources,
228-
IAM_PAGES_PERMISSIONS[IAM_PAGES.ADD_BUCKETS]
229-
);
222+
return hasPermission("*", IAM_PAGES_PERMISSIONS[IAM_PAGES.ADD_BUCKETS]);
230223
},
231224
},
232225
{

0 commit comments

Comments
 (0)