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

Storage ACL samples. #172

Merged
merged 2 commits into from
Aug 18, 2016
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
36 changes: 36 additions & 0 deletions storage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ amount of data at any time.

* [Setup](#setup)
* [Samples](#samples)
* [ACL (Access Control Lists)](#acl-access-control-lists)
* [Buckets](#buckets)
* [Encryption](#encryption)
* [Files](#files)
Expand All @@ -28,6 +29,41 @@ amount of data at any time.

## Samples

### ACL (Access Control Lists)

View the [documentation][acl_docs] or the [source code][acl_code].

__Usage:__ `node acl --help`

```
Commands:
Copy link
Contributor

Choose a reason for hiding this comment

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

Wishlist/Pipe dream: auto-generate these.

Copy link
Member Author

@jmdobry jmdobry Aug 18, 2016

Choose a reason for hiding this comment

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

You mean auto-generate all of README.md? The usage information was generated, I just ran node acl --help and pasted the output into README.md. I used to have to write all that by hand, now most of README.md is generated.

Copy link
Contributor

Choose a reason for hiding this comment

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

Generated in the "I don't have to even paste things in, I just run a script" sense.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think there's negligible work to be saved by scripting the copy paste. I mean, it could be scripted, but we'd need to devise a manifest.json file or something that could give the script meta information about the samples in the directory. Maybe it's not such a bad idea...

add <entity> <role> Add access controls on a bucket or file.
get [entity] Get access controls on a bucket or file.
delete <entity> Delete access controls from a bucket or file.

Options:
--bucket, -b The target storage bucket. [string] [required]
--default, -d Whether to set default access controls. Only valid when setting access controls on
a bucket. [boolean]
--file, -f The target file. [string]
--help Show help [boolean]

Examples:
node acl add user-bob@domain.com OWNER -b mybucket Add OWNER access controls for
"user-bob@domain.com" to "mybucket".
node acl add viewers-2256 WRITER -b mybucket -d Add default WRITER access controls to
"mybucket" for "viewers-2256".
node acl get editors-1234 -b mybucket Get access controls for "editors-1234" in
"mybucket".
node acl delete -b mybucket -f file.txt Delete all access controls for all entities
from "file.txt" in "mybucket".

For more information, see https://cloud.google.com/storage/docs/access-control/create-manage-lists
```

[acl_docs]: https://cloud.google.com/storage/docs/access-control/create-manage-lists
[acl_code]: acl.js

### Buckets

View the [documentation][buckets_docs] or the [source code][buckets_code].
Expand Down
218 changes: 218 additions & 0 deletions storage/acl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
// Copyright 2015-2016, Google, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// [START all]
// [START setup]
// By default, the client will authenticate using the service account file
// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
// the project specified by the GCLOUD_PROJECT environment variable. See
// https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/guides/authentication
var Storage = require('@google-cloud/storage');

// Instantiate a storage client
var storage = Storage();
// [END setup]

// [START add_access_control]
/**
* Add access controls to a bucket or file.
*
* @param {object} options Configuration options.
* @param {string} options.bucket The bucket for the new access controls.
* @param {string} options.entity The entity for the new access controls.
* @param {string} options.role The role for the new access controls.
* @param {string} [options.file] Optional. The file for the new access controls.
* @param {boolean} [options.default] Optional. Whether to set default access controls.
* @param {function} callback The callback function.
*/
function addAccessControl (options, callback) {
// Reference the specified storage bucket
var bucket = storage.bucket(options.bucket);
// Reference the bucket's acl resource
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/storage/bucket?method=acl.add
var acl = bucket.acl;

if (options.file) {
// Optionally target a file's acl resource
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/storage/file?method=acl.add
acl = bucket.file(options.file).acl;
} else if (options.default) {
// Optionally add "default" access controls to the bucket
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/storage/bucket?method=acl.default.add
acl = acl.default;
}

// Specify the entity and role for the new access control object
var config = {
entity: options.entity,
role: options.role
};

acl.add(config, function (err, aclObject) {
if (err) {
return callback(err);
}

console.log('Added access controls to: gs://%s' + (options.file ? '/%s' : ''), options.bucket, options.file || '');
return callback(null, aclObject);
});
}
// [END add_access_control]

// [START get_access_control]
/**
* Get access controls for a bucket or file.
*
* @param {object} options Configuration options.
* @param {string} options.bucket The bucket to target.
* @param {string} [options.entity] Optional. The entity to filter by.
* @param {string} [options.file] Optional. The file to target.
* @param {boolean} [options.default] Optional. Whether to get default access controls.
* @param {function} callback The callback function.
*/
function getAccessControl (options, callback) {
// Reference the specified storage bucket
var bucket = storage.bucket(options.bucket);
// Reference the bucket's acl resource
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/storage/bucket?method=acl.get
var acl = bucket.acl;

if (options.file) {
// Optionally target a file's acl resource
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/storage/file?method=acl.get
acl = bucket.file(options.file).acl;
} else if (options.default) {
// Optionally get "default" access controls for the bucket
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/storage/bucket?method=acl.default.get
acl = acl.default;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm assuming bucket.acl and bucket.acl.default are two different things. (If not, I may have an issue with this.)

Copy link
Member Author

Choose a reason for hiding this comment

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

They definitely are two different things.

Copy link
Member Author

@jmdobry jmdobry Aug 18, 2016

Choose a reason for hiding this comment

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

I added links in the source code to the relevant gcloud-node methods (with the "latest" version in the URL path).

}

if (options.entity) {
// Get a list of access controls for a specific entity
acl.get({ entity: options.entity }, handleResponse);
} else {
// Get a list of all access controls
acl.get(handleResponse);
}

function handleResponse (err, aclObject) {
if (err) {
return callback(err);
}

console.log('Got access controls for: gs://%s' + (options.file ? '/%s' : ''), options.bucket, options.file || '');
return callback(null, aclObject);
}
}
// [END get_access_control]

// [START delete_access_control]
/**
* Delete access controls from a bucket or file.
*
* @param {object} options Configuration options.
* @param {string} options.bucket The bucket to target.
* @param {string} options.entity The entity whose access is to be revoked.
* @param {string} [options.file] Optional. The file to target.
* @param {boolean} [options.default] Optional. Whether to delete default access controls.
* @param {function} callback The callback function.
*/
function deleteAccessControl (options, callback) {
// Reference the specified storage bucket
var bucket = storage.bucket(options.bucket);
// Reference the bucket's acl resource
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/storage/bucket?method=acl.delete
var acl = bucket.acl;

if (options.file) {
// Optionally target a file's acl resource
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/storage/file?method=acl.delete
acl = bucket.file(options.file).acl;
} else if (options.default) {
// Optionally delete "default" access controls from the bucket
// See https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/storage/bucket?method=acl.default.delete
acl = acl.default;
}

// Delete access controls for a specific entity
acl.delete({ entity: options.entity }, function (err) {
if (err) {
return callback(err);
}

console.log('Deleted access controls from: gs://%s' + (options.file ? '/%s' : ''), options.bucket, options.file || '');
return callback(null);
});
}
// [END delete_access_control]
// [END all]

// The command-line program
var cli = require('yargs');

var program = module.exports = {
addAccessControl: addAccessControl,
getAccessControl: getAccessControl,
deleteAccessControl: deleteAccessControl,
main: function (args) {
// Run the command-line program
cli.help().strict().parse(args).argv;
}
};

cli
.command('add <entity> <role>', 'Add access controls on a bucket or file.', {}, function (options) {
program.addAccessControl(options, console.log);
})
.command('get [entity]', 'Get access controls on a bucket or file.', {}, function (options) {
program.getAccessControl(options, console.log);
})
.command('delete <entity>', 'Delete access controls from a bucket or file.', {}, function (options) {
program.deleteAccessControl(options, console.log);
})
.example('node $0 add user-bob@domain.com OWNER -b mybucket', 'Add OWNER access controls for "user-bob@domain.com" to "mybucket".')
.example('node $0 add viewers-2256 WRITER -b mybucket -d', 'Add default WRITER access controls to "mybucket" for "viewers-2256".')
.example('node $0 get editors-1234 -b mybucket', 'Get access controls for "editors-1234" in "mybucket".')
.example('node $0 delete -b mybucket -f file.txt', 'Delete all access controls for all entities from "file.txt" in "mybucket".')
.options({
bucket: {
alias: 'b',
global: true,
demand: true,
requiresArg: true,
type: 'string',
description: 'The target storage bucket.'
},
default: {
alias: 'd',
global: true,
type: 'boolean',
description: 'Whether to set default access controls. Only valid when setting access controls on a bucket.'
},
file: {
alias: 'f',
global: true,
requiresArg: true,
type: 'string',
description: 'The target file.'
}
})
.wrap(100)
.recommendCommands()
.epilogue('For more information, see https://cloud.google.com/storage/docs/access-control/create-manage-lists');

if (module === require.main) {
program.main(process.argv.slice(2));
}
Empty file added storage/iam.js
Empty file.
3 changes: 2 additions & 1 deletion storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"dependencies": {
"@google-cloud/storage": "^0.1.1",
"googleapis": "^12.2.0",
"moment": "^2.14.1"
"moment": "^2.14.1",
"yargs": "^5.0.0"
},
"devDependencies": {
"mocha": "^3.0.2",
Expand Down
Loading