4

I've written an Golang program designed to is parse a scv file and upload data to FireStore, this program was build to share with people who just write scv path to upload info.

I'm using this to use firebase-admin:

opt := option.WithCredentialsFile("path/to/serviceAccountKey.json")

This approach works fine when I do:

$ go run main.go

but when I build the project and execute the binary I got this:

$ cannot read credentials file: open firebase-config.json: no such file or directory 

I did read this approach but it means share firebase-config.json too, the idea of this Go program is just share the executable without any doc else.

Is there a way to build the program including the json or authenticate firebase-admin using json directly like this?:

opt := option.WithCredentials({< authentication json >})

I have no found documentation or examples using this method WithCredential.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

4 Answers4

1

I don't know if this is the best way to solve this but this is the way I did it:

  1. I decided to place this script executable into /opt/< dir-name >/bin
  2. I wrote an extra function which creates the auth .json /opt/< dir-name >/auth.json
  3. All the script executes using this /opt/< dir-name >/auth.json file
  4. I write another func which deletes the auth.json when the execution ends

So, now I can do this:

var file string = "/opt/< dir-name >/auth.json" 
sa := option.WithCredentialsFile(file)

Now I can share this script with the directive "Install into /opt/< dir-name>/bin and add this to your $PATH"

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
1

For me, I use $GOPATH to fix this issue

  1. For example my $GOPATH is /home/Frank/work
  2. I put my .json file at /home/Frank/work/myfile.json
  3. I change my code to

    opt := option.WithCredentialsFile("/home/Frank/work/myfile.json")

And somehow it works. :D

Frank Nguyen
  • 6,493
  • 3
  • 38
  • 37
0

I was running into this exact issue with firebase-admin when trying to move my Google Cloud Functions written with Node to Google Cloud Run (GCR) using Go. Specifically, the error Error initializing app: cannot read credentials file: open ./firebase.json: no such file or directory

I'm not sure why, but when I pushed my container to GCR, the JSON file was lost and I was unable to connect to firebase-admin. I'm guessing it's much like you're saying, that Go programs, in theory, should be self-contained.

I believe I found a solution to this problem that suits my needs, I hope it helps you.

opt := option.WithCredentialsJSON([]byte(`{"some": "json"}`))

This does mean shoving a JSON file into one string, which absolutely should not work in all situations. However, with how small the firebase.json files generally are, It should be OK for this use case IMO.

Links that lead me down this path, left at the end in case these links change or break:

theGiantOtter
  • 146
  • 2
  • 10
0

Adding credentials to your code is not safe, the info can be leaked through your app or source code.

Instead, use up the environment variable GOOGLE_APPLICATION_CREDENTIALS as described in the Firebase setup documentation:

You can either set the GOOGLE_APPLICATION_CREDENTIALS environment variable, or you can explicitly pass the path to the service account key in code. The first option is more secure and is strongly recommended.

Step 1: Remove WithCredentialsFile() call

Since the environment variable will be automatically used, you do not need to specifically load the credentials file, just call NewApp():

app, err := firebase.NewApp(context.Background(), nil)
if err != nil {
        log.Fatalf("error initializing app: %v\n", err)
}

Step 2: Set environment variable

Set the environment variable before running the code:

$ export GOOGLE_APPLICATION_CREDENTIALS="./conf/solarsink-firebase-adminsdk.json"
$ go run .

If you are using VSCode you can also add this environment variable to launch.json:

    "configurations": [
        {
            "name": "test",
            "type": "go",
            "request": "launch",
            "mode": "test",
            "program": "${workspaceFolder}",
            "args": [
                "-test.run",
                "TestStatus"
            ],
            "env": {
                "GOOGLE_APPLICATION_CREDENTIALS": "./conf/solarsink-firebase-adminsdk.json"
              }
        }

    ]
Chris Novak
  • 579
  • 5
  • 5