Skip to content

Commit

Permalink
Merge pull request #252 from mrdomino/public-url-base
Browse files Browse the repository at this point in the history
landscape: add public url base to S3 configuration
  • Loading branch information
arthyn authored Jan 22, 2024
2 parents f2c068d + 740646a commit f6eddc0
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 20 deletions.
37 changes: 30 additions & 7 deletions desk/app/storage.hoon
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
$% state-zero
state-one
state-two
state-three
==
::
+$ state-zero [%0 =credentials:zero:past =configuration:zero:past]
+$ state-one [%1 =credentials:one:past =configuration:one:past]
+$ state-two [%2 =credentials =configuration]
+$ state-zero [%0 =credentials:zero:past =configuration:zero:past]
+$ state-one [%1 =credentials:one:past =configuration:one:past]
+$ state-two [%2 =credentials:two:past =configuration:two:past]
+$ state-three [%3 =credentials =configuration]
--
::
=| state-two
=| state-three
=* state -
::
%- agent:dbug
Expand Down Expand Up @@ -49,9 +51,10 @@
=/ old u.old
|-
?- -.old
%0 $(old (state-0-to-1 old))
%3 `this(state old)
%2 $(old (state-2-to-3 old))
%1 $(old (state-1-to-2 old))
%2 `this(state old)
%0 $(old (state-0-to-1 old))
==
++ state-0-to-1
|= zer=state-zero
Expand Down Expand Up @@ -79,13 +82,30 @@
::
++ configuration-1-to-2
|= conf=configuration:one:past
^- ^configuration
^- configuration:two:past
:* buckets.conf
current-bucket.conf
region.conf
''
%credentials
==
++ state-2-to-3
|= two=state-two
^- state-three
:* %3
credentials.two
(configuration-2-to-3 configuration.two)
==
++ configuration-2-to-3
|= conf=configuration:two:past
^- ^configuration
:* buckets.conf
current-bucket.conf
region.conf
presigned-url.conf
service.conf
''
==
--
::
++ on-poke
Expand Down Expand Up @@ -138,6 +158,9 @@
::
%set-region
state(region.configuration region.act)
::
%set-public-url-base
state(public-url-base.configuration public-url-base.act)
::
%set-current-bucket
%_ state
Expand Down
19 changes: 11 additions & 8 deletions desk/lib/storage-json.hoon
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
[%set-access-key-id so:dejs]
[%set-secret-access-key so:dejs]
[%set-region so:dejs]
[%set-public-url-base so:dejs]
[%add-bucket so:dejs]
[%remove-bucket so:dejs]
[%set-current-bucket so:dejs]
Expand All @@ -26,14 +27,15 @@
%+ frond:enjs %storage-update
%- pairs:enjs
:~ ?- -.upd
%set-current-bucket [%'setCurrentBucket' s+bucket.upd]
%add-bucket [%'addBucket' s+bucket.upd]
%set-region [%'setRegion' s+region.upd]
%remove-bucket [%'removeBucket' s+bucket.upd]
%set-endpoint [%'setEndpoint' s+endpoint.upd]
%set-access-key-id [%'setAccessKeyId' s+access-key-id.upd]
%set-presigned-url [%'setPresignedUrl' s+url.upd]
%toggle-service [%'toggleService' s+service.upd]
%set-current-bucket [%'setCurrentBucket' s+bucket.upd]
%add-bucket [%'addBucket' s+bucket.upd]
%set-region [%'setRegion' s+region.upd]
%set-public-url-base [%'setPublicUrlBase' s+public-url-base.upd]
%remove-bucket [%'removeBucket' s+bucket.upd]
%set-endpoint [%'setEndpoint' s+endpoint.upd]
%set-access-key-id [%'setAccessKeyId' s+access-key-id.upd]
%set-presigned-url [%'setPresignedUrl' s+url.upd]
%toggle-service [%'toggleService' s+service.upd]
%set-secret-access-key
[%'setSecretAccessKey' s+secret-access-key.upd]
::
Expand All @@ -51,6 +53,7 @@
:~ [%buckets a+(turn ~(tap in buckets.configuration.upd) |=(a=@t s+a))]
[%'currentBucket' s+current-bucket.configuration.upd]
[%'region' s+region.configuration.upd]
[%'publicUrlBase' s+public-url-base.configuration.upd]
[%'service' s+service.configuration.upd]
[%'presignedUrl' s+presigned-url.configuration.upd]
==
Expand Down
42 changes: 42 additions & 0 deletions desk/sur/storage-2.hoon
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
|%
+$ service ?(%presigned-url %credentials)
+$ credentials
$: endpoint=@t
access-key-id=@t
secret-access-key=@t
==
::
:: $configuration: the upload configuration
::
:: $buckets: the buckets available
:: $current-bucket: the current bucket we use to upload
:: $region: the region of the current bucket
:: $presigned-url: the presigned url endpoint
:: $service: whether to use a presigned url service or direct S3 uploads
::
+$ configuration
$: buckets=(set @t)
current-bucket=@t
region=@t
presigned-url=@t
=service
==
::
+$ action
$% [%set-endpoint endpoint=@t]
[%set-access-key-id access-key-id=@t]
[%set-secret-access-key secret-access-key=@t]
[%add-bucket bucket=@t]
[%remove-bucket bucket=@t]
[%set-current-bucket bucket=@t]
[%set-region region=@t]
[%set-presigned-url url=@t]
[%toggle-service =service]
==
::
+$ update
$% [%credentials =credentials]
[%configuration =configuration]
action
==
--
6 changes: 5 additions & 1 deletion desk/sur/storage.hoon
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/- zer=storage-0, uno=storage-1
/- zer=storage-0, uno=storage-1, dos=storage-2
|%
++ past
|%
++ zero zer
++ one uno
++ two dos
--
+$ service ?(%presigned-url %credentials)
+$ credentials
Expand All @@ -19,13 +20,15 @@
:: $region: the region of the current bucket
:: $presigned-url: the presigned url endpoint
:: $service: whether to use a presigned url service or direct S3 uploads
:: $public-url-base: URL base to substitute into returned object URLs
::
+$ configuration
$: buckets=(set @t)
current-bucket=@t
region=@t
presigned-url=@t
=service
public-url-base=@t
==
::
+$ action
Expand All @@ -36,6 +39,7 @@
[%remove-bucket bucket=@t]
[%set-current-bucket bucket=@t]
[%set-region region=@t]
[%set-public-url-base public-url-base=@t]
[%set-presigned-url url=@t]
[%toggle-service =service]
==
Expand Down
8 changes: 8 additions & 0 deletions ui/src/gear/storage/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
StorageUpdateAccessKeyId,
StorageUpdateSecretAccessKey,
StorageUpdateRegion,
StorageUpdatePublicUrlBase,
StorageService,
StorageUpdateSetPresignedUrl,
StorageUpdateToggleService,
Expand Down Expand Up @@ -60,6 +61,13 @@ export const setRegion = (region: string): Poke<StorageUpdateRegion> =>
'set-region': region,
});

export const setPublicUrlBase = (
publicUrlBase: string
): Poke<StorageUpdatePublicUrlBase> =>
storageAction({
'set-public-url-base': publicUrlBase,
});

export const setPresignedUrl = (
presignedUrl: string
): Poke<StorageUpdateSetPresignedUrl> =>
Expand Down
6 changes: 6 additions & 0 deletions ui/src/gear/storage/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface StorageConfiguration {
buckets: Set<string>;
currentBucket: string;
region: string;
publicUrlBase: string;
presignedUrl: string;
service: StorageService;
}
Expand Down Expand Up @@ -135,6 +136,10 @@ export interface StorageUpdateRegion {
setRegion: string;
}

export interface StorageUpdatePublicUrlBase {
setPublicUrlBase: string;
}

export interface StorageUpdateToggleService {
toggleService: string;
}
Expand All @@ -153,5 +158,6 @@ export declare type StorageUpdate =
| StorageUpdateAccessKeyId
| StorageUpdateSecretAccessKey
| StorageUpdateRegion
| StorageUpdatePublicUrlBase
| StorageUpdateToggleService
| StorageUpdateSetPresignedUrl;
20 changes: 20 additions & 0 deletions ui/src/preferences/StoragePrefs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ interface CredentialsSubmit {
accessId: string;
accessSecret: string;
region: string;
publicUrlBase: string;
bucket: string;
}

type S3Update =
| { 'set-region': string }
| { 'set-public-url-base': string }
| { 'set-endpoint': string }
| { 'set-access-key-id': string }
| { 'set-secret-access-key': string }
Expand Down Expand Up @@ -60,6 +62,7 @@ export const StoragePrefs = () => {
api.poke(storagePoke({ 'set-secret-access-key': data.accessSecret }));
api.poke(storagePoke({ 'set-current-bucket': data.bucket }));
api.poke(storagePoke({ 'set-region': data.region }));
api.poke(storagePoke({ 'set-public-url-base': data.publicUrlBase }));
}, [])
);

Expand Down Expand Up @@ -207,6 +210,23 @@ export const StoragePrefs = () => {
{!loaded && <Spinner className="absolute top-1 right-2" />}
</div>
</div>
<div className="mb-8 flex flex-col space-y-2">
<label className="font-semibold" htmlFor="publicUrlBase">
Public URL base (normally unnecessary)
</label>
<div className="relative">
<input
disabled={!loaded}
id="publicUrlBase"
type="text"
autoCorrect="off"
defaultValue={s3.configuration?.publicUrlBase}
{...register('publicUrlBase')}
className="input default-ring bg-gray-50"
/>
{!loaded && <Spinner className="absolute top-1 right-2" />}
</div>
</div>
<div className="mb-8 flex flex-col space-y-2">
<label className="font-semibold" htmlFor="bucket">
Bucket Name<span title="Required field">*</span>
Expand Down
12 changes: 12 additions & 0 deletions ui/src/state/storage/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const configuration = (
buckets: new Set(data.buckets),
currentBucket: data.currentBucket,
region: data.region,
publicUrlBase: data.publicUrlBase,
// if landscape is not up to date we need to default these so the
// client init logic still works
presignedUrl: data.presignedUrl || hostingUploadURL,
Expand Down Expand Up @@ -105,6 +106,16 @@ const region = (json: StorageUpdate, state: StorageState): StorageState => {
return state;
};

const publicUrlBase = (
json: StorageUpdate, state: StorageState
): StorageState => {
const data = _.get(json, 'setPublicUrlBase', false);
if (data && state.s3.configuration) {
state.s3.configuration.publicUrlBase = data;
}
return state;
};

const presignedUrl = (
json: StorageUpdate,
state: StorageState
Expand Down Expand Up @@ -137,6 +148,7 @@ const reduce = [
accessKeyId,
secretAccessKey,
region,
publicUrlBase,
presignedUrl,
toggleService,
];
Expand Down
1 change: 1 addition & 0 deletions ui/src/state/storage/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const useStorage = createState<BaseStorageState>(
buckets: new Set(),
currentBucket: '',
region: '',
publicUrlBase: '',
presignedUrl: hostingUploadURL,
service: 'credentials',
},
Expand Down
9 changes: 5 additions & 4 deletions ui/src/state/storage/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,18 @@ export const useFileStore = create<FileStore>((set, get) => ({
ACL: 'public-read',
});

const url = await getSignedUrl(client, command);
const url = config.publicUrlBase
? new URL(key, config.publicUrlBase).toString()
: await getSignedUrl(client, command).then((res) => res.split('?')[0]);

client
.send(command)
.then(() => {
const fileUrl = url.split('?')[0];
updateStatus(uploader, key, 'success');
imageSize(fileUrl).then((s) =>
imageSize(url).then((s) =>
updateFile(uploader, key, {
size: s,
url: fileUrl,
url,
})
);
})
Expand Down

0 comments on commit f6eddc0

Please sign in to comment.