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

Add w3 CLI script for w3s driver #17

Merged
merged 7 commits into from
Feb 7, 2023
Merged

Add w3 CLI script for w3s driver #17

merged 7 commits into from
Feb 7, 2023

Conversation

leszko
Copy link
Contributor

@leszko leszko commented Feb 3, 2023

fix livepeer/catalyst#383
kudos to @red-0ne for starting this work in his PoC PR: livepeer/catalyst-api#387

Context
In the w3s driver, we use w3 CLI to interact with web3.storage. With the current implementation, we use the credentials created by w3 CLI (stored in the local FS). In other words, currently, we don't use the credentials provided by the user, which is not correct and not what we need for web3.storage integration.

What we want is to pass the credentials (UCAN private key configured with the catalyst-api flag and the delegation proof passed by the user) into w3 CLI and use them to interact with the web3.storage service. There are, however, two issues:

  1. w3 CLI does not provide a way to import the private key (though it allows importing deletion proofs)
  2. w3 CLI stores its credentials (and its state) in the local FS; at the same time we need multi-tenancy because different users need to use w3 with different proofs.

Solution
This PR adds a JS script that can be used in the same way as w3 CLI for our use cases, but it allows passing private key and poof as env variables. So, instead of executing w3 can store add <car-file>, we will execute the following command.

$ W3_PRINCIPAL_KEY=<key> W3_DELEGATION_PROOF=<proof> node w3.mjs can store add <car-file>

Additional comments:

  • credentials are stored in memory, so there is no problem with multi-tenancy since each execution is separated
  • apart from that, we'll need to update the catalyst Dockerfile to install node and npm dependencies
  • we can consider moving this script to livepeer/catalyst or livepeer/catalyst-api

Alternative solutions

1. Implement this as a feature in w3 CLI

The best for us would be if this is actually implemented in the w3 CLI. I've sent a PR to their repo (storacha/w3cli#42). I'm not sure it'll be merged, but let's try it.

2. Import credentials into w3 CLI

We could write a script (@red-0ne already wrote it here) and then use w3 profiles for multi-tenancy.

Nevertheless, I think that solution is worse because it would still mean maintaining our own "import" script and additionally:

  • we would need to take care of the credentials clean-up
  • credentials would be stored in the FS (which is always worse then in-memory from the security point of view)

3. Fork w3 CLI

We could fork the w3cli repo and maintain our own version, but I'm not sure it's worth it.

4. Move this script to a separate repo

Instead of keeping this script in livepeer/go-tools (or livepeer/catalyst / livepeer/catalyst-api), we could create a separate repo and store it as a complete JS module (with package.json, etc.).

@leszko leszko marked this pull request as ready for review February 3, 2023 15:57
Copy link
Member

@victorges victorges left a comment

Choose a reason for hiding this comment

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

LGTM either way. We can even keep this script on this separate branch until we figure this out better.

re: distributing to catalyst, have you given a try on tools like pkg? These should create a hopefully distributable binary to run the node script. Might be simpler than adding node support in catalyst container (if we don't have that yet)

scripts/w3.mjs Outdated Show resolved Hide resolved
Copy link
Member

@victorges victorges left a comment

Choose a reason for hiding this comment

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

LGTM!

drivers/w3s.go Outdated
return nil, fmt.Errorf("invalid UCAN proof format: %s", err)
}
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, fmt.Sprintf("W3_PRINCIPAL_KEY='%s'", W3sUcanKey))
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if there is an opp to simplify how this key is configured. I see that right now the flow is:

  • Pass value in a flag for a catalyst-api
  • Set the package var W3sUcanKey with the value
  • Read W3sUcanKey and set it as an env var when calling the livepeer-w3 script

An alternative might be:

  • Set W3_PRINCIPAL_KEY in the environment that catalyst-api is running in i.e. in the Docker container
  • livepeer-w3 just reads the W3_PRINCIPAL_KEY env var value directly without any additional value passing which reduces the # of code paths that have to be aware of this value

I see we've already merged the relevant catalyst-api PRs so maybe consider whether this might be a worthwhile simplification separately.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My initial thinking was that not storing the private key in the environment variable is more secure because even if you access the container, you still cannot access it.

However, now I think you're right because anyway we pass the key in the env variable to catalyst. So, maybe let's get rid of the Catalyst flag and just use the private key passed in the W3_PRINCIPAL_KEY. Thanks for this comment @yondonfu

@leszko
Copy link
Contributor Author

leszko commented Feb 7, 2023

LGTM either way. We can even keep this script on this separate branch until we figure this out better.

re: distributing to catalyst, have you given a try on tools like pkg? These should create a hopefully distributable binary to run the node script. Might be simpler than adding node support in catalyst container (if we don't have that yet)

I tried pkg, but actually encountered some issues while packing these dependencies. So, we'll probably need to stick with installing nodejs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add authentication and multi-tenancy to w3s driver
4 participants