Skip to content

Commit

Permalink
Merge pull request #5 from loft-sh/fix/json_cred_env
Browse files Browse the repository at this point in the history
fix: support passing Gcloud JSON credentials via Env
  • Loading branch information
pascalbreuninger authored Sep 26, 2023
2 parents 68fd485 + c05a115 commit 6e91faf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"context"

"github.com/loft-sh/devpod-provider-gcloud/pkg/gcloud"
"github.com/loft-sh/devpod-provider-gcloud/pkg/options"
"github.com/loft-sh/devpod/pkg/log"
Expand Down
29 changes: 26 additions & 3 deletions pkg/gcloud/gcloud.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
package gcloud

import (
compute "cloud.google.com/go/compute/apiv1"
computepb "cloud.google.com/go/compute/apiv1/computepb"
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"

compute "cloud.google.com/go/compute/apiv1"
computepb "cloud.google.com/go/compute/apiv1/computepb"
"github.com/googleapis/gax-go/v2/apierror"
"github.com/loft-sh/devpod/pkg/client"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/googleapi"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
"strings"
)

func NewClient(ctx context.Context, project, zone string, opts ...option.ClientOption) (*Client, error) {
err := SetupEnvJson(ctx)
if err != nil {
return nil, err
}

instanceClient, err := compute.NewInstancesRESTClient(ctx, opts...)
if err != nil {
return nil, err
Expand All @@ -36,6 +44,21 @@ type Client struct {
Zone string
}

func SetupEnvJson(ctx context.Context) error {
if os.Getenv("GCLOUD_JSON_AUTH") != "" {
destination := filepath.Join(os.TempDir(), "gcloud_auth.json")

err := os.WriteFile(destination, []byte(os.Getenv("GCLOUD_JSON_AUTH")), 0o400)
if err != nil {
return err
}

return os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", destination)
}

return nil
}

func DefaultTokenSource(ctx context.Context) (oauth2.TokenSource, error) {
return google.DefaultTokenSource(ctx)
}
Expand Down

0 comments on commit 6e91faf

Please sign in to comment.