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

Edit service access control #47

Merged
merged 2 commits into from
Nov 14, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions content/pages/editService.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,20 @@
"type": "checkbox",
"options": ["This asset uses user defined parameters"],
"required": false
},
{
"name": "allow",
"label": "Allow ETH Address",
"placeholder": "e.g. 0xe328aB96B7CbB55A6E1c1054678137bA09780acA",
"help": "Enter an ETH address and click the ADD button to append to the list. Only ETH addresses in the allow list can consume this service. If the list is empty anyone can use this service.",
"type": "credentials"
},
{
"name": "deny",
"label": "Deny ETH Address",
"placeholder": "e.g. 0xe328aB96B7CbB55A6E1c1054678137bA09780acA",
"help": "Enter an ETH address and click the ADD button to append to the list. If an ETH address is in the deny list, download or compute of this service will be denied for that ETH address.",
"type": "credentials"
}
]
}
Expand Down
288 changes: 266 additions & 22 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@coingecko/cryptoformat": "^0.5.4",
"@loadable/component": "^5.15.3",
"@oceanprotocol/art": "^3.2.0",
"@oceanprotocol/lib": "^3.3.3",
"@oceanprotocol/lib": "^3.4.3",
"@oceanprotocol/typographies": "^0.1.0",
"@oceanprotocol/use-dark-mode": "^2.4.3",
"@tippyjs/react": "^4.2.6",
Expand Down
12 changes: 11 additions & 1 deletion src/components/Asset/Edit/AddService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import { useAsset } from '@context/Asset'
import { setNftMetadata } from '@utils/nft'
import { getEncryptedFiles } from '@utils/provider'
import { useAccount, useNetwork, useSigner } from 'wagmi'
import { transformConsumerParameters } from '@components/Publish/_utils'
import {
generateCredentials,
transformConsumerParameters
} from '@components/Publish/_utils'
import {
defaultDatatokenCap,
defaultDatatokenTemplateIndex,
Expand Down Expand Up @@ -160,6 +163,12 @@ export default function AddService({
newFiles = filesEncrypted
}

const credentials = generateCredentials(
undefined,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually optional parameters should be last

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking what to do about it, but perhaps it's best to just keep it like that. It is used like that even in the code that I haven't changed e.g. here https://github.com/OceanProtocolEnterprise/market/blob/main/src/components/Publish/_utils.ts#L242
LMK what you think

values.allow,
values.deny
)

const newService: Service = {
id: getHash(datatokenAddress + newFiles),
type: values.access,
Expand All @@ -169,6 +178,7 @@ export default function AddService({
datatokenAddress,
serviceEndpoint: values.providerUrl.url,
timeout: mapTimeoutStringToSeconds(values.timeout),
credentials,
...(values.access === 'compute' && {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we use enums, to not have string encoded?

compute: await transformComputeFormToServiceComputeOptions(
values,
Expand Down
16 changes: 13 additions & 3 deletions src/components/Asset/Edit/DebugEditService.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Asset, LoggerInstance, Service } from '@oceanprotocol/lib'
import { Asset, Credentials, LoggerInstance, Service } from '@oceanprotocol/lib'
import { ReactElement, useEffect, useState } from 'react'
import DebugOutput from '@shared/DebugOutput'
import { useCancelToken } from '@hooks/useCancelToken'
Expand All @@ -10,7 +10,10 @@ import {
previewDebugPatch
} from '@utils/ddo'
import { getEncryptedFiles } from '@utils/provider'
import { transformConsumerParameters } from '@components/Publish/_utils'
import {
generateCredentials,
transformConsumerParameters
} from '@components/Publish/_utils'

export default function DebugEditService({
values,
Expand Down Expand Up @@ -53,13 +56,20 @@ export default function DebugEditService({
LoggerInstance.error('Error encrypting files:', error.message)
}

const credentials: Credentials = generateCredentials(
service.credentials,
values.allow,
values.deny
)

const updatedService: Service = {
...service,
name: values.name,
description: values.description,
type: values.access,
timeout: mapTimeoutStringToSeconds(values.timeout),
files: updatedFiles, // TODO: check if this works
files: updatedFiles, // TODO: check if this works,
credentials,
...(values.access === 'compute' && {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here. enums?

compute: await transformComputeFormToServiceComputeOptions(
values,
Expand Down
14 changes: 12 additions & 2 deletions src/components/Asset/Edit/EditService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import { useAsset } from '@context/Asset'
import { setNftMetadata } from '@utils/nft'
import { getEncryptedFiles } from '@utils/provider'
import { useAccount, useNetwork, useSigner } from 'wagmi'
import { transformConsumerParameters } from '@components/Publish/_utils'
import {
generateCredentials,
transformConsumerParameters
} from '@components/Publish/_utils'
import FormEditService from './FormEditService'
import { transformComputeFormToServiceComputeOptions } from '@utils/compute'
import { useCancelToken } from '@hooks/useCancelToken'
Expand Down Expand Up @@ -103,12 +106,19 @@ export default function EditService({
updatedFiles = filesEncrypted
}

const updatedCredentials = generateCredentials(
service.credentials,
values.allow,
values.deny
)

const updatedService: Service = {
...service,
name: values.name,
description: values.description,
timeout: mapTimeoutStringToSeconds(values.timeout),
files: updatedFiles, // TODO: check if this works
files: updatedFiles, // TODO: check if this works,
credentials: updatedCredentials,
...(values.access === 'compute' && {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

compute: await transformComputeFormToServiceComputeOptions(
values,
Expand Down
7 changes: 7 additions & 0 deletions src/components/Asset/Edit/FormAddService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ export default function FormAddService({
name="timeout"
/>

<Field
{...getFieldContent('allow', data)}
component={Input}
name="allow"
/>
<Field {...getFieldContent('deny', data)} component={Input} name="deny" />

<Field
{...getFieldContent('usesConsumerParameters', data)}
component={Input}
Expand Down
7 changes: 7 additions & 0 deletions src/components/Asset/Edit/FormEditService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ export default function FormEditService({
name="timeout"
/>

<Field
{...getFieldContent('allow', data)}
component={Input}
name="allow"
/>
<Field {...getFieldContent('deny', data)} component={Input} name="deny" />

<Field
{...getFieldContent('usesConsumerParameters', data)}
component={Input}
Expand Down
10 changes: 10 additions & 0 deletions src/components/Asset/Edit/_constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export const getNewServiceInitialValues = (
timeout: '1 day',
usesConsumerParameters: false,
consumerParameters: [],
allow: [],
deny: [],
...computeSettings
}
}
Expand All @@ -105,6 +107,14 @@ export const getServiceInitialValues = (
timeout: secondsToString(service.timeout),
usesConsumerParameters: service.consumerParameters?.length > 0,
consumerParameters: parseConsumerParameters(service.consumerParameters),
allow:
service.credentials?.allow?.find(
(credential) => credential.type === 'address'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe enum?

)?.values || [],
deny:
service.credentials?.deny?.find(
(credential) => credential.type === 'address'
)?.values || [],
...computeSettings
}
}
2 changes: 2 additions & 0 deletions src/components/Asset/Edit/_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export interface ServiceEditForm {
timeout: string
usesConsumerParameters: boolean
consumerParameters: FormConsumerParameter[]
allow: string[]
deny: string[]
// compute
allowAllPublishedAlgorithms: boolean
publisherTrustedAlgorithms: string[]
Expand Down
4 changes: 3 additions & 1 deletion src/components/Asset/Edit/_validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,7 @@ export const serviceValidationSchema = Yup.object().shape({
),
allowAllPublishedAlgorithms: Yup.boolean().nullable(),
publisherTrustedAlgorithms: Yup.array().nullable(),
publisherTrustedAlgorithmPublishers: Yup.array().nullable()
publisherTrustedAlgorithmPublishers: Yup.array().nullable(),
allow: Yup.array().of(Yup.string()).nullable(),
deny: Yup.array().of(Yup.string()).nullable()
})
2 changes: 1 addition & 1 deletion src/components/Publish/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export function transformConsumerParameters(
}

export function generateCredentials(
oldCredentials: Credentials,
oldCredentials: Credentials | undefined,
updatedAllow: string[],
updatedDeny: string[]
): Credentials {
Expand Down