Allow credentials to be set from base64 encoded envvar, instead of filesystem.

This commit is contained in:
Patrick Connolly
2018-10-08 17:55:20 +08:00
parent 7e1d9ed8c2
commit aac7cffbdf
2 changed files with 13 additions and 2 deletions

View File

@@ -97,6 +97,11 @@ See [howto](https://github.com/42wim/matterbridge/wiki/How-to-create-your-config
## Advanced configuration ## Advanced configuration
* [matterbridge.toml.sample](https://github.com/42wim/matterbridge/blob/master/matterbridge.toml.sample) for documentation and an example. * [matterbridge.toml.sample](https://github.com/42wim/matterbridge/blob/master/matterbridge.toml.sample) for documentation and an example.
* If you'd like to allow channels to be Google Translated between
languages, then simply set an environment variable called
`GOOGLE_APPLICATION_CREDENTIALS_BASE64` to be the base64 encoded version
of the JSON credentials file.
## Examples ## Examples
### Bridge mattermost (off-topic) - irc (#testing) ### Bridge mattermost (off-topic) - irc (#testing)
``` ```

View File

@@ -2,6 +2,7 @@ package gateway
import ( import (
"bytes" "bytes"
b64 "encoding/base64"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@@ -27,6 +28,8 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
// "github.com/davecgh/go-spew/spew" // "github.com/davecgh/go-spew/spew"
"cloud.google.com/go/translate" "cloud.google.com/go/translate"
"google.golang.org/api/option"
"golang.org/x/oauth2/google"
"golang.org/x/text/language" "golang.org/x/text/language"
"crypto/sha1" "crypto/sha1"
"path/filepath" "path/filepath"
@@ -268,11 +271,14 @@ func (gw *Gateway) handleMessage(msg config.Message, dest *bridge.Bridge) []*BrM
msg.Avatar = gw.modifyAvatar(origmsg, dest) msg.Avatar = gw.modifyAvatar(origmsg, dest)
msg.Username = gw.modifyUsername(origmsg, dest) msg.Username = gw.modifyUsername(origmsg, dest)
msg.ID = "" msg.ID = ""
if channel.Options.Locale != "" { credsEnc, ok := os.LookupEnv("GOOGLE_APPLICATION_CREDENTIALS_BASE64")
if ok && channel.Options.Locale != "" {
ctx := context.Background() ctx := context.Background()
lang, _ := language.Parse(channel.Options.Locale) lang, _ := language.Parse(channel.Options.Locale)
client, _ := translate.NewClient(ctx) credsDec, _ := b64.StdEncoding.DecodeString(credsEnc)
creds, _ := google.CredentialsFromJSON(ctx, credsDec, translate.Scope)
client, _ := translate.NewClient(ctx, option.WithCredentials(creds))
defer client.Close() defer client.Close()
resp, _ := client.Translate(ctx, []string{msg.Text}, lang, nil) resp, _ := client.Translate(ctx, []string{msg.Text}, lang, nil)