From e17ca0810060c73e633986b9028974328bf2534e Mon Sep 17 00:00:00 2001 From: Gus Class Date: Wed, 27 Sep 2017 11:01:14 -0700 Subject: [PATCH 1/2] Removes API key now that discovery is public --- iot/manager/manager.js | 77 +++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 42 deletions(-) diff --git a/iot/manager/manager.js b/iot/manager/manager.js index 53dad96b9d..d9cb6d3f0b 100644 --- a/iot/manager/manager.js +++ b/iot/manager/manager.js @@ -88,7 +88,7 @@ function createIotTopic (topicName) { function lookupRegistry (client, registryId, projectId, cloudRegion, cb) { // [START iot_lookup_registry] // Client retrieved in callback - // getClient(apiKey, serviceAccountJson, function(client) {...}); + // getClient(serviceAccountJson, function(client) {...}); // const cloudRegion = 'us-central1'; // const projectId = 'adjective-noun-123'; // const registryId = 'my-registry'; @@ -114,7 +114,7 @@ function createRegistry (client, registryId, projectId, cloudRegion, pubsubTopicId, foundCb) { // [START iot_create_registry] // Client retrieved in callback - // getClient(apiKey, serviceAccountJson, function(client) {...}); + // getClient(serviceAccountJson, function(client) {...}); // const cloudRegion = 'us-central1'; // const projectId = 'adjective-noun-123'; // const registryId = 'my-registry'; @@ -154,7 +154,7 @@ function lookupOrCreateRegistry (client, registryId, projectId, cloudRegion, pubsubTopicId) { // [START iot_lookup_or_create_registry] // Client retrieved in callback - // getClient(apiKey, serviceAccountJson, function(client) {...}); + // getClient(serviceAccountJson, function(client) {...}); // const cloudRegion = 'us-central1'; // const projectId = 'adjective-noun-123'; // const registryId = 'my-registry'; @@ -172,7 +172,7 @@ function createUnauthDevice (client, deviceId, registryId, projectId, cloudRegion, body) { // [START iot_create_unauth_device] // Client retrieved in callback - // getClient(apiKey, serviceAccountJson, function(client) {...}); + // getClient(serviceAccountJson, function(client) {...}); // const cloudRegion = 'us-central1'; // const deviceId = 'my-unauth-device'; // const projectId = 'adjective-noun-123'; @@ -203,7 +203,7 @@ function createRsaDevice (client, deviceId, registryId, projectId, cloudRegion, rsaCertificateFile) { // [START iot_create_rsa_device] // Client retrieved in callback - // getClient(apiKey, serviceAccountJson, function(client) {...}); + // getClient(serviceAccountJson, function(client) {...}); // const cloudRegion = 'us-central1'; // const deviceId = 'my-rsa-device'; // const projectId = 'adjective-noun-123'; @@ -246,7 +246,7 @@ function createEsDevice (client, deviceId, registryId, projectId, cloudRegion, esCertificateFile) { // [START iot_create_es_device] // Client retrieved in callback - // getClient(apiKey, serviceAccountJson, function(client) {...}); + // getClient(serviceAccountJson, function(client) {...}); // const cloudRegion = 'us-central1'; // const deviceId = 'my-es-device'; // const projectId = 'adjective-noun-123'; @@ -287,7 +287,7 @@ function patchRsa256ForAuth (client, deviceId, registryId, rsaPublicKeyFile, projectId, cloudRegion) { // [START iot_patch_rsa] // Client retrieved in callback - // getClient(apiKey, serviceAccountJson, function(client) {...}); + // getClient(serviceAccountJson, function(client) {...}); // const cloudRegion = 'us-central1'; // const deviceId = 'my-rsa-device'; // const projectId = 'adjective-noun-123'; @@ -327,7 +327,7 @@ function patchEs256ForAuth (client, deviceId, registryId, esPublicKeyFile, projectId, cloudRegion) { // [START iot_patch_es] // Client retrieved in callback - // getClient(apiKey, serviceAccountJson, function(client) {...}); + // getClient(serviceAccountJson, function(client) {...}); // const cloudRegion = 'us-central1'; // const deviceId = 'my-es-device'; // const projectId = 'adjective-noun-123'; @@ -366,7 +366,7 @@ function patchEs256ForAuth (client, deviceId, registryId, esPublicKeyFile, function listDevices (client, registryId, projectId, cloudRegion) { // [START iot_list_devices] // Client retrieved in callback - // getClient(apiKey, serviceAccountJson, function(client) {...}); + // getClient(serviceAccountJson, function(client) {...}); // const cloudRegion = 'us-central1'; // const projectId = 'adjective-noun-123'; // const registryId = 'my-registry'; @@ -392,7 +392,7 @@ function listDevices (client, registryId, projectId, cloudRegion) { function listRegistries (client, projectId, cloudRegion) { // [START iot_list_registries] // Client retrieved in callback - // getClient(apiKey, serviceAccountJson, function(client) {...}); + // getClient(serviceAccountJson, function(client) {...}); // const cloudRegion = 'us-central1'; // const projectId = 'adjective-noun-123'; const parentName = `projects/${projectId}/locations/${cloudRegion}`; @@ -417,7 +417,7 @@ function deleteDevice (client, deviceId, registryId, projectId, cloudRegion, cb) { // [START iot_delete_device] // Client retrieved in callback - // getClient(apiKey, serviceAccountJson, function(client) {...}); + // getClient(serviceAccountJson, function(client) {...}); // const cloudRegion = 'us-central1'; // const projectId = 'adjective-noun-123'; // const registryId = 'my-registry'; @@ -496,7 +496,7 @@ function clearRegistry (client, registryId, projectId, cloudRegion) { function deleteRegistry (client, registryId, projectId, cloudRegion) { // [START iot_delete_registry] // Client retrieved in callback - // getClient(apiKey, serviceAccountJson, function(client) {...}); + // getClient(serviceAccountJson, function(client) {...}); // const cloudRegion = 'us-central1'; // const projectId = 'adjective-noun-123'; // const registryId = 'my-registry'; @@ -522,7 +522,7 @@ function deleteRegistry (client, registryId, projectId, cloudRegion) { function getDevice (client, deviceId, registryId, projectId, cloudRegion) { // [START iot_get_device] // Client retrieved in callback - // getClient(apiKey, serviceAccountJson, function(client) {...}); + // getClient(serviceAccountJson, function(client) {...}); // const cloudRegion = 'us-central1'; // const deviceId = 'my-device'; // const projectId = 'adjective-noun-123'; @@ -550,7 +550,7 @@ function getDeviceState (client, deviceId, registryId, projectId, cloudRegion) { // [START iot_get_device_state] // Client retrieved in callback - // getClient(apiKey, serviceAccountJson, function(client) {...}); + // getClient(serviceAccountJson, function(client) {...}); // const cloudRegion = 'us-central1'; // const deviceId = 'my-device'; // const projectId = 'adjective-noun-123'; @@ -577,7 +577,7 @@ function getDeviceState (client, deviceId, registryId, projectId, function getRegistry (client, registryId, projectId, cloudRegion) { // [START iot_get_registry] // Client retrieved in callback - // getClient(apiKey, serviceAccountJson, function(client) {...}); + // getClient(serviceAccountJson, function(client) {...}); // const cloudRegion = 'us-central1'; // const projectId = 'adjective-noun-123'; // const registryId = 'my-registry'; @@ -601,7 +601,7 @@ function getRegistry (client, registryId, projectId, cloudRegion) { // Returns an authorized API client by discovering the Cloud IoT Core API with // the provided API key. -function getClient (apiKey, serviceAccountJson, cb) { +function getClient (serviceAccountJson, cb) { const serviceAccount = JSON.parse(fs.readFileSync(serviceAccountJson)); const jwtAccess = new google.auth.JWT(); jwtAccess.fromJSON(serviceAccount); @@ -611,7 +611,7 @@ function getClient (apiKey, serviceAccountJson, cb) { // Set the default authentication to the above JWT access. google.options({ auth: jwtAccess }); - const discoveryUrl = `${DISCOVERY_API}?version=${API_VERSION}&key=${apiKey}`; + const discoveryUrl = `${DISCOVERY_API}?version=${API_VERSION}`; google.discoverAPI(discoveryUrl, {}, (err, client) => { if (err) { @@ -625,13 +625,6 @@ function getClient (apiKey, serviceAccountJson, cb) { require(`yargs`) // eslint-disable-line .demand(1) .options({ - apiKey: { - alias: 'a', - default: process.env.API_KEY, - description: 'The API key used for discoverying the API.', - requiresArg: true, - type: 'string' - }, cloudRegion: { alias: 'c', default: 'us-central1', @@ -662,7 +655,7 @@ require(`yargs`) // eslint-disable-line createRsaDevice(client, opts.deviceId, opts.registryId, opts.projectId, opts.cloudRegion, opts.rsaPath); }; - getClient(opts.apiKey, opts.serviceAccount, cb); + getClient(opts.serviceAccount, cb); } ) .command( @@ -674,7 +667,7 @@ require(`yargs`) // eslint-disable-line createEsDevice(client, opts.deviceId, opts.registryId, opts.projectId, opts.cloudRegion, opts.esPath); }; - getClient(opts.apiKey, opts.serviceAccount, cb); + getClient(opts.serviceAccount, cb); } ) .command( @@ -686,7 +679,7 @@ require(`yargs`) // eslint-disable-line createUnauthDevice(client, opts.deviceId, opts.registryId, opts.projectId, opts.cloudRegion); }; - getClient(opts.apiKey, opts.serviceAccount, cb); + getClient(opts.serviceAccount, cb); } ) .command( @@ -698,7 +691,7 @@ require(`yargs`) // eslint-disable-line lookupOrCreateRegistry(client, opts.registryId, opts.projectId, opts.cloudRegion, opts.pubsubTopic); }; - getClient(opts.apiKey, opts.serviceAccount, cb); + getClient(opts.serviceAccount, cb); } ) .command( @@ -722,7 +715,7 @@ require(`yargs`) // eslint-disable-line deleteDevice(client, opts.deviceId, opts.registryId, opts.projectId, opts.cloudRegion); }; - getClient(opts.apiKey, opts.serviceAccount, cb); + getClient(opts.serviceAccount, cb); } ) .command( @@ -734,7 +727,7 @@ require(`yargs`) // eslint-disable-line clearRegistry(client, opts.registryId, opts.projectId, opts.cloudRegion); }; - getClient(opts.apiKey, opts.serviceAccount, cb); + getClient(opts.serviceAccount, cb); } ) .command( @@ -746,7 +739,7 @@ require(`yargs`) // eslint-disable-line deleteRegistry(client, opts.registryId, opts.projectId, opts.cloudRegion); }; - getClient(opts.apiKey, opts.serviceAccount, cb); + getClient(opts.serviceAccount, cb); } ) .command( @@ -758,7 +751,7 @@ require(`yargs`) // eslint-disable-line getDevice(client, opts.deviceId, opts.registryId, opts.projectId, opts.cloudRegion); }; - getClient(opts.apiKey, opts.serviceAccount, cb); + getClient(opts.serviceAccount, cb); } ) .command( @@ -770,7 +763,7 @@ require(`yargs`) // eslint-disable-line getDeviceState(client, opts.deviceId, opts.registryId, opts.projectId, opts.cloudRegion); }; - getClient(opts.apiKey, opts.serviceAccount, cb); + getClient(opts.serviceAccount, cb); } ) .command( @@ -781,7 +774,7 @@ require(`yargs`) // eslint-disable-line const cb = function (client) { getRegistry(client, opts.registryId, opts.projectId, opts.cloudRegion); }; - getClient(opts.apiKey, opts.serviceAccount, cb); + getClient(opts.serviceAccount, cb); } ) .command( @@ -792,7 +785,7 @@ require(`yargs`) // eslint-disable-line const cb = function (client) { listDevices(client, opts.registryId, opts.projectId, opts.cloudRegion); }; - getClient(opts.apiKey, opts.serviceAccount, cb); + getClient(opts.serviceAccount, cb); } ) .command( @@ -803,7 +796,7 @@ require(`yargs`) // eslint-disable-line const cb = function (client) { listRegistries(client, opts.projectId, opts.cloudRegion); }; - getClient(opts.apiKey, opts.serviceAccount, cb); + getClient(opts.serviceAccount, cb); } ) .command( @@ -815,7 +808,7 @@ require(`yargs`) // eslint-disable-line patchEs256ForAuth(client, opts.deviceId, opts.registryId, opts.es256Path, opts.projectId, opts.cloudRegion); }; - getClient(opts.apiKey, opts.serviceAccount, cb); + getClient(opts.serviceAccount, cb); } ) .command( @@ -827,12 +820,12 @@ require(`yargs`) // eslint-disable-line patchRsa256ForAuth(client, opts.deviceId, opts.registryId, opts.rsa256Path, opts.projectId, opts.cloudRegion); }; - getClient(opts.apiKey, opts.serviceAccount, cb); + getClient(opts.serviceAccount, cb); } ) - .example(`node $0 createEs256Device my-es-device my-registry ../ec_public.pem --apiKey=abc123zz`) - .example(`node $0 createRegistry my-registry my-iot-topic --service_account_json=$HOME/creds_iot.json --api_key=abc123zz --project_id=my-project-id`) - .example(`node $0 createRsa256Device my-rsa-device my-registry ../rsa_cert.pem --apiKey=abc123zz`) + .example(`node $0 createEs256Device my-es-device my-registry ../ec_public.pem`) + .example(`node $0 createRegistry my-registry my-iot-topic --service_account_json=$HOME/creds_iot.json --project_id=my-project-id`) + .example(`node $0 createRsa256Device my-rsa-device my-registry ../rsa_cert.pem`) .example(`node $0 createUnauthDevice my-device my-registry`) .example(`node $0 deleteDevice my-device my-registry`) .example(`node $0 deleteRegistry my-device my-registry`) @@ -843,7 +836,7 @@ require(`yargs`) // eslint-disable-line .example(`node $0 listRegistries`) .example(`node $0 patchRsa256 my-device my-registry ../rsa_cert.pem`) .example(`node $0 patchEs256 my-device my-registry ../ec_public.pem`) - .example(`node $0 setupTopic my-iot-topic --service_account_json=$HOME/creds_iot.json --api_key=abc123zz --project_id=my-project-id`) + .example(`node $0 setupTopic my-iot-topic --service_account_json=$HOME/creds_iot.json --project_id=my-project-id`) .wrap(120) .recommendCommands() .epilogue(`For more information, see https://cloud.google.com/iot-core/docs`) From 725b6d13d33a8a94c2fa795e89bdb2ca46d124dd Mon Sep 17 00:00:00 2001 From: Gus Class Date: Wed, 27 Sep 2017 13:54:32 -0700 Subject: [PATCH 2/2] Places usage example in code block --- iot/manager/README.md | 80 +++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/iot/manager/README.md b/iot/manager/README.md index f29821be08..d39419f923 100644 --- a/iot/manager/README.md +++ b/iot/manager/README.md @@ -15,43 +15,43 @@ Run the following command to install the library dependencies for NodeJS: # Running the sample -Commands: - createRsa256Device Creates an RSA256 device. - createEs256Device Creates an ES256 device. - createUnauthDevice Creates a device without authorization. - createRegistry Creates a device registry. - createIotTopic Creates and configures a PubSub topic for Cloud IoT Core. - setupIotTopic Configures the PubSub topic for Cloud IoT Core. - deleteDevice Deletes a device from the device registry. - clearRegistry !!Be careful! Removes all devices and then deletes a device - registry!! - deleteRegistry Deletes a device registry. - getDevice Retrieves device info given a device ID. - listDevices Lists the devices in a given registry. - patchEs256 Patches a device with ES256 authorization credentials. - patchRsa256 Patches a device with RSA256 authentication credentials. - -Options: - --apiKey, -a The API key used for discoverying the API. [string] - --projectId, -p The Project ID to use. Defaults to the value of the GCLOUD_PROJECT or GOOGLE_CLOUD_PROJECT - environment variables. [string] - --serviceAccount, -s The path to your service credentials JSON. [string] - --help Show help [boolean] - --cloudRegion, -c [string] [default: "us-central1"] - -Examples: - node manager.js createEs256Device my-es-device my-registry ../ec_public.pem --apiKey=abc123zz - node manager.js createRegistry my-registry my-iot-topic --service_account_json=$HOME/creds_iot.json - --api_key=abc123zz --project_id=my-project-id - node manager.js createRsa256Device my-rsa-device my-registry ../rsa_cert.pem --apiKey=abc123zz - node manager.js createUnauthDevice my-device my-registry - node manager.js deleteDevice my-device my-registry - node manager.js deleteRegistry my-device my-registry - node manager.js getDevice my-device my-registry - node manager.js listDevices my-node-registry - node manager.js patchRsa256 my-device my-registry ../rsa_cert.pem - node manager.js patchEs256 my-device my-registry ../ec_public.pem - node manager.js setupTopic my-iot-topic --service_account_json=$HOME/creds_iot.json --api_key=abc123zz - --project_id=my-project-id - -For more information, see https://cloud.google.com/iot-core/docs + Commands: + createRsa256Device Creates an RSA256 device. + createEs256Device Creates an ES256 device. + createUnauthDevice Creates a device without authorization. + createRegistry Creates a device registry. + createIotTopic Creates and configures a PubSub topic for Cloud IoT Core. + setupIotTopic Configures the PubSub topic for Cloud IoT Core. + deleteDevice Deletes a device from the device registry. + clearRegistry !!Be careful! Removes all devices and then deletes a device + registry!! + deleteRegistry Deletes a device registry. + getDevice Retrieves device info given a device ID. + listDevices Lists the devices in a given registry. + patchEs256 Patches a device with ES256 authorization credentials. + patchRsa256 Patches a device with RSA256 authentication credentials. + + Options: + --apiKey, -a The API key used for discoverying the API. [string] + --projectId, -p The Project ID to use. Defaults to the value of the GCLOUD_PROJECT or GOOGLE_CLOUD_PROJECT + environment variables. [string] + --serviceAccount, -s The path to your service credentials JSON. [string] + --help Show help [boolean] + --cloudRegion, -c [string] [default: "us-central1"] + + Examples: + node manager.js createEs256Device my-es-device my-registry ../ec_public.pem --apiKey=abc123zz + node manager.js createRegistry my-registry my-iot-topic --service_account_json=$HOME/creds_iot.json + --api_key=abc123zz --project_id=my-project-id + node manager.js createRsa256Device my-rsa-device my-registry ../rsa_cert.pem --apiKey=abc123zz + node manager.js createUnauthDevice my-device my-registry + node manager.js deleteDevice my-device my-registry + node manager.js deleteRegistry my-device my-registry + node manager.js getDevice my-device my-registry + node manager.js listDevices my-node-registry + node manager.js patchRsa256 my-device my-registry ../rsa_cert.pem + node manager.js patchEs256 my-device my-registry ../ec_public.pem + node manager.js setupTopic my-iot-topic --service_account_json=$HOME/creds_iot.json --api_key=abc123zz + --project_id=my-project-id + + For more information, see https://cloud.google.com/iot-core/docs