-
Notifications
You must be signed in to change notification settings - Fork 378
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
feat: add auto auth features #281
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b507652
feat: add auto auth features
JustinBeckwith a03516e
shuffle samples about
JustinBeckwith 4033164
Merge branch 'master' into auto1
JustinBeckwith 5e7049f
Merge branch 'master' into auto1
JustinBeckwith c800934
Merge branch 'master' into auto1
JustinBeckwith 0241109
pr feedback
JustinBeckwith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2018, Google, LLC. | ||
// 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'; | ||
|
||
/** | ||
* Import the GoogleAuth library, and create a new GoogleAuth client. | ||
*/ | ||
const { auth } = require('google-auth-library'); | ||
const axios = require('axios'); | ||
|
||
/** | ||
* Acquire a client, and make a request to an API that's enabled by default. | ||
*/ | ||
async function main() { | ||
const client = await auth.getClient(); | ||
const projectId = await auth.getDefaultProjectId(); | ||
const url = `https://www.googleapis.com/dns/v1/projects/${projectId}`; | ||
const opts = await auth.authorizeRequest(); | ||
const res = await axios.get(url, opts); | ||
console.log(res.data); | ||
} | ||
|
||
main().catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2018, Google, LLC. | ||
// 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'; | ||
|
||
/** | ||
* Import the GoogleAuth library, and create a new GoogleAuth client. | ||
*/ | ||
const { GoogleAuth } = require('google-auth-library'); | ||
|
||
/** | ||
* Acquire a client, and make a request to an API that's enabled by default. | ||
*/ | ||
async function main() { | ||
const credentials = require('jwt.keys.json'); | ||
const scopes = 'https://www.googleapis.com/auth/cloud-platform'; | ||
const auth = new GoogleAuth({ credentials, scopes }); | ||
const client = await auth.getClient(); | ||
const projectId = await auth.getDefaultProjectId(); | ||
const url = `https://www.googleapis.com/dns/v1/projects/${projectId}`; | ||
const res = await client.request({ url }); | ||
console.log(res.data); | ||
} | ||
|
||
main().catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2018, Google, LLC. | ||
// 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'; | ||
|
||
/** | ||
* Import the GoogleAuth library, and create a new GoogleAuth client. | ||
*/ | ||
const { GoogleAuth } = require('google-auth-library'); | ||
|
||
/** | ||
* Acquire a client, and make a request to an API that's enabled by default. | ||
*/ | ||
async function main() { | ||
const auth = new GoogleAuth({ | ||
keyFilename: 'jwt.keys.json', | ||
scopes: 'https://www.googleapis.com/auth/cloud-platform' | ||
}); | ||
const client = await auth.getClient(); | ||
const projectId = await auth.getDefaultProjectId(); | ||
const url = `https://www.googleapis.com/dns/v1/projects/${projectId}`; | ||
const res = await client.request({ url }); | ||
console.log(res.data); | ||
} | ||
|
||
main().catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* Copyright 2018 Google LLC. All Rights Reserved. | ||
* | ||
* 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. | ||
*/ | ||
|
||
import * as gcpMetadata from 'gcp-metadata'; | ||
|
||
export enum GCPEnv { | ||
APP_ENGINE = 'APP_ENGINE', | ||
KUBERNETES_ENGINE = 'KUBERNETES_ENGINE', | ||
CLOUD_FUNCTIONS = 'CLOUD_FUNCTIONS', | ||
COMPUTE_ENGINE = 'COMPUTE_ENGINE', | ||
NONE = 'NONE' | ||
} | ||
|
||
let env: GCPEnv|undefined; | ||
|
||
export function clear() { | ||
env = undefined; | ||
} | ||
|
||
export async function getEnv() { | ||
if (!env) { | ||
if (isAppEngine()) { | ||
env = GCPEnv.APP_ENGINE; | ||
} else if (isCloudFunction()) { | ||
env = GCPEnv.CLOUD_FUNCTIONS; | ||
} else if (await isKubernetesEngine()) { | ||
env = GCPEnv.KUBERNETES_ENGINE; | ||
} else if (await isComputeEngine()) { | ||
env = GCPEnv.COMPUTE_ENGINE; | ||
} else { | ||
env = GCPEnv.NONE; | ||
} | ||
} | ||
return env; | ||
} | ||
|
||
function isAppEngine() { | ||
return !!(process.env.GAE_SERVICE || process.env.GAE_MODULE_NAME); | ||
} | ||
|
||
function isCloudFunction() { | ||
return !!process.env.FUNCTION_NAME; | ||
} | ||
|
||
async function isKubernetesEngine() { | ||
try { | ||
await gcpMetadata.instance('attributes/cluster-name'); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
async function isComputeEngine() { | ||
return gcpMetadata.isAvailable(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.