You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

96 line
3.1 KiB

  1. // Copyright 2014 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package testutil contains helper functions for writing tests.
  15. package testutil
  16. import (
  17. "fmt"
  18. "io/ioutil"
  19. "log"
  20. "os"
  21. "golang.org/x/net/context"
  22. "golang.org/x/oauth2"
  23. "golang.org/x/oauth2/google"
  24. "golang.org/x/oauth2/jwt"
  25. )
  26. const (
  27. envProjID = "GCLOUD_TESTS_GOLANG_PROJECT_ID"
  28. envPrivateKey = "GCLOUD_TESTS_GOLANG_KEY"
  29. )
  30. // ProjID returns the project ID to use in integration tests, or the empty
  31. // string if none is configured.
  32. func ProjID() string {
  33. return os.Getenv(envProjID)
  34. }
  35. // TokenSource returns the OAuth2 token source to use in integration tests,
  36. // or nil if none is configured. It uses the standard environment variable
  37. // for tests in this repo.
  38. func TokenSource(ctx context.Context, scopes ...string) oauth2.TokenSource {
  39. return TokenSourceEnv(ctx, envPrivateKey, scopes...)
  40. }
  41. // TokenSourceEnv returns the OAuth2 token source to use in integration tests. or nil
  42. // if none is configured. It tries to get credentials from the filename in the
  43. // environment variable envVar. If the environment variable is unset, TokenSourceEnv
  44. // will try to find 'Application Default Credentials'. Else, TokenSourceEnv will
  45. // return nil. TokenSourceEnv will log.Fatal if the token source is specified but
  46. // missing or invalid.
  47. func TokenSourceEnv(ctx context.Context, envVar string, scopes ...string) oauth2.TokenSource {
  48. key := os.Getenv(envVar)
  49. if key == "" { // Try for application default credentials.
  50. ts, err := google.DefaultTokenSource(ctx, scopes...)
  51. if err != nil {
  52. log.Println("No 'Application Default Credentials' found.")
  53. return nil
  54. }
  55. return ts
  56. }
  57. conf, err := jwtConfigFromFile(key, scopes)
  58. if err != nil {
  59. log.Fatal(err)
  60. }
  61. return conf.TokenSource(ctx)
  62. }
  63. // JWTConfig reads the JSON private key file whose name is in the default
  64. // environment variable, and returns the jwt.Config it contains. It ignores
  65. // scopes.
  66. // If the environment variable is empty, it returns (nil, nil).
  67. func JWTConfig() (*jwt.Config, error) {
  68. return jwtConfigFromFile(os.Getenv(envPrivateKey), nil)
  69. }
  70. // jwtConfigFromFile reads the given JSON private key file, and returns the
  71. // jwt.Config it contains.
  72. // If the filename is empty, it returns (nil, nil).
  73. func jwtConfigFromFile(filename string, scopes []string) (*jwt.Config, error) {
  74. if filename == "" {
  75. return nil, nil
  76. }
  77. jsonKey, err := ioutil.ReadFile(filename)
  78. if err != nil {
  79. return nil, fmt.Errorf("Cannot read the JSON key file, err: %v", err)
  80. }
  81. conf, err := google.JWTConfigFromJSON(jsonKey, scopes...)
  82. if err != nil {
  83. return nil, fmt.Errorf("google.JWTConfigFromJSON: %v", err)
  84. }
  85. return conf, nil
  86. }