Repository navigation
Conversation
…ckage instead of AWS sig
|
CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅ |
|
I have read the CLA Document and I hereby sign the CLA |
Isakdl
left a comment
There was a problem hiding this comment.
Thank you very much for the PR @olivoil very helpful and good work putting this together 😄!
I have taken a look at the code and made a few suggestions. On top of these comments I would like to put this code into a complete new package rather than overriding the old one as you point out this is a breaking change. But if we create an additional package it is not.
On the tests, yes unfortunately we have 0 tests on this code today. There are two different approaches to test this, either using a mock for the google services, such as this https://github.com/fsouza/fake-gcs-server, or implement a real e2e test against a real bucket.
It would be good to have coverage here, but I understand that this may be difficult to setup and I can help you with that if you like.
| required Session session, | ||
| required String path, | ||
| String? subresource, | ||
| int expiration = 604800, |
There was a problem hiding this comment.
Nit: Make this magic number into a variable and declare it like 7 * 24 * 60 * 60 as it is much easier to digest the value this way.
| final instance = GoogleCloudStorage._( | ||
| serverpod: serverpod, | ||
| storageId: storageId, | ||
| public: public, | ||
| bucket: bucket, | ||
| serviceAccountPath: serviceAccountPath, | ||
| ); |
There was a problem hiding this comment.
Suggestion: the publicHost variable is not passed down here.
| required this.serviceAccountPath, | ||
| String? publicHost, | ||
| }) : super(storageId) { | ||
| this.publicHost = publicHost ?? 'storage.googleapis.com/$bucket'; |
There was a problem hiding this comment.
Suggestion: Set the publicHost as the default in the constructor instead. It shouldn't contain the /$bucket part either, add that part where it is relevant.
| required String storageId, | ||
| required bool public, | ||
| required String bucket, | ||
| required String serviceAccountPath, |
There was a problem hiding this comment.
Suggestion:
This comment comes with a few implications. By passing in a path to the service account we are forcing people to use a service account stored on disc to authenticate with google. But there are otherways people may want to auth such as via the metaserver in a hosted environment. Google also has a method for default authentication, e.g. clientViaApplicationDefaultCredentials that will use an environment variable with the path to the service account, or the metaserver if running in cloud engine.
So it would be nice to use this method instead of passing in the path to the service account.
On top of this, it would be great to take an optional http client. If set it would override the call to clientViaApplicationDefaultCredentials. Then the user can use any available Google auth client or even a mock client for testing.
There is one implication of this though, and that is that we are currently signing the URL in process because we have direct access to the private key. This is not possible if we have authed via the meta server.
Luckily Google has an API to sign the URL though, but it requires the service account email. We could in other words take the service account email as input instead of the service account path and call this endpoint.
Like so:
var _iamCredentials = IAMCredentialsApi(authedClient);
final response = await _iamCredentials.projects.serviceAccounts.signBlob(
SignBlobRequest(payload: base64.encode(stringToSignList)),
'projects/-/serviceAccounts/$_serviceAccountEmail',
);Where the the class IamCredentials comes from import 'package:googleapis/iamcredentials/v1.dart';
With the above change we can remove all service account management inside the code.
There was a problem hiding this comment.
I think using the API to get a signature makes sense, and removes the burden of maintaining the signature logic. I'll update it
|
|
||
| import 'package:crypto/crypto.dart'; | ||
| import 'package:hex/hex.dart'; | ||
| import 'package:gcloud/storage.dart'; |
There was a problem hiding this comment.
Suggestion: Reading up on this dependency, it is tagged as experimental by Google. I'm a little reluctant to add it because of that. It is also possible to implement this same functionality with googleapis/storage, it is a bigger deviation from the current solution, but I believe it would be more resilient.
There was a problem hiding this comment.
It does have a status of experimental, and I don't know what their timeline to GA is, or if it will make it to GA... Will look into googleapis/storage to see how it works
|
Hello, For the web integration, it’s necessary to configure CORS as follows: [
{
"origin": ["http://localhost:49423", [TO_BE_COMPLETED]],
"method": ["DELETE", "GET", "HEAD", "POST","PUT"],
"responseHeader": ["Content-Type", "x-goog-content-length-range"],
"maxAgeSeconds": 3600
}
]I created a file named gcloud_cors.json containing this configuration and applied it using the following command: I believe it could be beneficial to include this information in the project's documentation once the PR is merged. I'd be happy to contribute and help with that if needed. |
Thank you for the review! I'll post an update addressing the feedback, then we can look at how to test it @Isakdl any preference on what to name the package? Since |
|
@olivoil good question, but some variant sounds nice. Not 100% sure on the naming but ideally it should look nice and be kind of obvious that this is the one you should use. Some ideas: |
|
In which version of serverpod this will release because we really need to check is file uploaded or not and better control the security of files so each user only see files that belongs to them. |
|
Closing now that the issue is solved with the refactor of the storage packages. |
This PR refactors the implementation of the GoogleCloudStorage integration to use gcloud.
Authentication to Cloud Storage switches was previously done using HMAC keys, this PR uses service account credentials. Users are expected to provide the path to the credentials file when creating a GoogleCloudStorage instance.
Fixes #2517.
Alternate solution to #2523 which could be closed in favor of this PR.
This implementation is based on a gist I shared in #2517 (comment), which I have been using on an app in development. It's been adapted to the current version of Serverpod.
All existing tests seem to pass, as it runs
util/run_testswithout error. However, I think the s3 and gcp storage integrations likely need more tests. I would have expected at least one fail since I introduced a breaking change to the GoogleCloudStorage constructor (documented below). It may be good to add tests to both storage integrations.Pre-launch Checklist
///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.If you need help, consider asking for advice on the discussion board.
Breaking changes
The way to instantiate a GoogleCloudStorage instance is slightly different, as we need to run an
asyncfunction to read the service credentials file and setup authentication to gcloud.An example of usage has been added to the GoogleCloudStorage class:
Additionally, users are now expected to provide service account credentials instead of using HMAC keys to authenticate with storage. The documentation on the website likely needs to be updated to reflect it. I am assuming the website documentation falls outside of this PR, but happy to contribute to it if needed.