Figure out projectID for GCS automatically from credentials.json (#5029)

fixes #5027
This commit is contained in:
Krishna Srinivas 2017-10-20 13:59:12 -07:00 committed by Dee Koder
parent d82a1da511
commit 7e05b826fa
3 changed files with 69 additions and 4 deletions

View File

@ -24,7 +24,9 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"math" "math"
"os"
"regexp" "regexp"
"strings" "strings"
"time" "time"
@ -73,6 +75,9 @@ const (
// The cleanup routine deletes files older than 2 weeks in minio.sys.tmp // The cleanup routine deletes files older than 2 weeks in minio.sys.tmp
gcsMultipartExpiry = time.Hour * 24 * 14 gcsMultipartExpiry = time.Hour * 24 * 14
// Project ID key in credentials.json
gcsProjectIDKey = "project_id"
) )
// Stored in gcs.json - Contents of this file is not used anywhere. It can be // Stored in gcs.json - Contents of this file is not used anywhere. It can be
@ -257,11 +262,34 @@ type gcsGateway struct {
const googleStorageEndpoint = "storage.googleapis.com" const googleStorageEndpoint = "storage.googleapis.com"
// Returns projectID from the GOOGLE_APPLICATION_CREDENTIALS file.
func gcsParseProjectID(credsFile string) (projectID string, err error) {
contents, err := ioutil.ReadFile(credsFile)
if err != nil {
return projectID, err
}
googleCreds := make(map[string]string)
if err = json.Unmarshal(contents, &googleCreds); err != nil {
return projectID, err
}
return googleCreds[gcsProjectIDKey], err
}
// newGCSGateway returns gcs gatewaylayer // newGCSGateway returns gcs gatewaylayer
func newGCSGateway(projectID string) (GatewayLayer, error) { func newGCSGateway(projectID string) (GatewayLayer, error) {
ctx := context.Background() ctx := context.Background()
err := checkGCSProjectID(ctx, projectID) var err error
if projectID == "" {
// If project ID is not provided on command line, we figure it out
// from the credentials.json file.
projectID, err = gcsParseProjectID(os.Getenv("GOOGLE_APPLICATION_CREDENTIALS"))
if err != nil {
return nil, err
}
}
err = checkGCSProjectID(ctx, projectID)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -18,6 +18,8 @@ package cmd
import ( import (
"fmt" "fmt"
"io/ioutil"
"os"
"reflect" "reflect"
"testing" "testing"
@ -179,3 +181,30 @@ func TestFromMinioClientListBucketResultToV2Info(t *testing.T) {
t.Errorf("fromMinioClientListBucketResultToV2Info() = %v, want %v", got, listBucketV2Info) t.Errorf("fromMinioClientListBucketResultToV2Info() = %v, want %v", got, listBucketV2Info)
} }
} }
// Test for gcsParseProjectID
func TestGCSParseProjectID(t *testing.T) {
f, err := ioutil.TempFile("", "")
if err != nil {
t.Error(err)
return
}
defer os.Remove(f.Name())
defer f.Close()
contents := `
{
"type": "service_account",
"project_id": "miniotesting"
}
`
f.WriteString(contents)
projectID, err := gcsParseProjectID(f.Name())
if err != nil {
t.Error(err)
return
}
if projectID != "miniotesting" {
t.Errorf(`Expected projectID value to be "miniotesting"`)
}
}

View File

@ -99,13 +99,13 @@ const gcsGatewayTemplate = `NAME:
{{.HelpName}} - {{.Usage}} {{.HelpName}} - {{.Usage}}
USAGE: USAGE:
{{.HelpName}} {{if .VisibleFlags}}[FLAGS]{{end}} PROJECTID {{.HelpName}} {{if .VisibleFlags}}[FLAGS]{{end}} [PROJECTID]
{{if .VisibleFlags}} {{if .VisibleFlags}}
FLAGS: FLAGS:
{{range .VisibleFlags}}{{.}} {{range .VisibleFlags}}{{.}}
{{end}}{{end}} {{end}}{{end}}
PROJECTID: PROJECTID:
GCS project id, there are no defaults this is mandatory. GCS project-id should be provided if GOOGLE_APPLICATION_CREDENTIALS environmental variable is not set.
ENVIRONMENT VARIABLES: ENVIRONMENT VARIABLES:
ACCESS: ACCESS:
@ -115,6 +115,9 @@ ENVIRONMENT VARIABLES:
BROWSER: BROWSER:
MINIO_BROWSER: To disable web browser access, set this value to "off". MINIO_BROWSER: To disable web browser access, set this value to "off".
GCS credentials file:
GOOGLE_APPLICATION_CREDENTIALS: Path to credentials.json
EXAMPLES: EXAMPLES:
1. Start minio gateway server for GCS backend. 1. Start minio gateway server for GCS backend.
$ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json $ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json
@ -319,7 +322,12 @@ func gcsGatewayMain(ctx *cli.Context) {
cli.ShowCommandHelpAndExit(ctx, "gcs", 1) cli.ShowCommandHelpAndExit(ctx, "gcs", 1)
} }
if !isValidGCSProjectIDFormat(ctx.Args().First()) { projectID := ctx.Args().First()
if projectID == "" && os.Getenv("GOOGLE_APPLICATION_CREDENTIALS") == "" {
errorIf(errGCSProjectIDNotFound, "project-id should be provided as argument or GOOGLE_APPLICATION_CREDENTIALS should be set with path to credentials.json")
cli.ShowCommandHelpAndExit(ctx, "gcs", 1)
}
if projectID != "" && !isValidGCSProjectIDFormat(projectID) {
errorIf(errGCSInvalidProjectID, "Unable to start GCS gateway with %s", ctx.Args().First()) errorIf(errGCSInvalidProjectID, "Unable to start GCS gateway with %s", ctx.Args().First())
cli.ShowCommandHelpAndExit(ctx, "gcs", 1) cli.ShowCommandHelpAndExit(ctx, "gcs", 1)
} }