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

Removes API key now that discovery is public #492

Merged
merged 7 commits into from
Sep 29, 2017
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
80 changes: 40 additions & 40 deletions iot/manager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,43 @@ Run the following command to install the library dependencies for NodeJS:

# Running the sample

Commands:
createRsa256Device <deviceId> <registryId> <rsaPath> Creates an RSA256 device.
createEs256Device <deviceId> <registryId> <esPath> Creates an ES256 device.
createUnauthDevice <deviceId> <registryId> Creates a device without authorization.
createRegistry <registryId> <pubsubTopic> Creates a device registry.
createIotTopic <pubsubTopic> Creates and configures a PubSub topic for Cloud IoT Core.
setupIotTopic <pubsubTopic> Configures the PubSub topic for Cloud IoT Core.
deleteDevice <deviceId> <registryId> Deletes a device from the device registry.
clearRegistry <registryId> !!Be careful! Removes all devices and then deletes a device
registry!!
deleteRegistry <registryId> Deletes a device registry.
getDevice <deviceId> <registryId> Retrieves device info given a device ID.
listDevices <registryId> Lists the devices in a given registry.
patchEs256 <deviceId> <registryId> <es256Path> Patches a device with ES256 authorization credentials.
patchRsa256 <deviceId> <registryId> <rsa256Path> 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 <deviceId> <registryId> <rsaPath> Creates an RSA256 device.
createEs256Device <deviceId> <registryId> <esPath> Creates an ES256 device.
createUnauthDevice <deviceId> <registryId> Creates a device without authorization.
createRegistry <registryId> <pubsubTopic> Creates a device registry.
createIotTopic <pubsubTopic> Creates and configures a PubSub topic for Cloud IoT Core.
setupIotTopic <pubsubTopic> Configures the PubSub topic for Cloud IoT Core.
deleteDevice <deviceId> <registryId> Deletes a device from the device registry.
clearRegistry <registryId> !!Be careful! Removes all devices and then deletes a device
registry!!
deleteRegistry <registryId> Deletes a device registry.
getDevice <deviceId> <registryId> Retrieves device info given a device ID.
listDevices <registryId> Lists the devices in a given registry.
patchEs256 <deviceId> <registryId> <es256Path> Patches a device with ES256 authorization credentials.
patchRsa256 <deviceId> <registryId> <rsa256Path> 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
77 changes: 35 additions & 42 deletions iot/manager/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand All @@ -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}`;
Expand All @@ -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';
Expand Down Expand Up @@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -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';
Expand All @@ -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';
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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',
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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`)
Expand All @@ -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`)
Expand Down