Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for fetcing private key from an environment variable #40

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions appsTransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"time"

Expand Down Expand Up @@ -36,6 +37,15 @@ func NewAppsTransportKeyFromFile(tr http.RoundTripper, appID int64, privateKeyFi
return NewAppsTransport(tr, appID, privateKey)
}

// NewAppsTransportKeyFromEnv returns a AppsTransport using a private key from environment variable.
func NewAppsTransportKeyFromEnv(tr http.RoundTripper, appID int64, privateKeyEnv string) (*AppsTransport, error) {
privateKey, ok := os.LookupEnv(privateKeyEnv)
if !ok {
return nil, fmt.Errorf("private key environment variable %s empty", privateKeyEnv)
}
return NewAppsTransport(tr, appID, []byte(privateKey))
}

// NewAppsTransport returns a AppsTransport using private key. The key is parsed
// and if any errors occur the error is non-nil.
//
Expand Down
11 changes: 11 additions & 0 deletions appsTransport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ func TestNewAppsTransportKeyFromFile(t *testing.T) {
}
}

func TestNewAppsTransportKeyFromEnv(t *testing.T) {
if err := os.Setenv("KEY", string(key)); err != nil {
t.Fatal("unexpected error:", err)
}

_, err := NewAppsTransportKeyFromEnv(&http.Transport{}, appID, "KEY")
if err != nil {
t.Fatal("unexpected error:", err)
}
}

type RoundTrip struct {
rt func(*http.Request) (*http.Response, error)
}
Expand Down
10 changes: 10 additions & 0 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"io/ioutil"
"net/http"
"os"
"sync"
"time"

Expand Down Expand Up @@ -60,6 +61,15 @@ func NewKeyFromFile(tr http.RoundTripper, appID, installationID int64, privateKe
return New(tr, appID, installationID, privateKey)
}

// NewKeyFromFile returns a Transport using a private key from environment variable.
func NewKeyFromEnv(tr http.RoundTripper, appID, installationID int64, privateKeyEnv string) (*Transport, error) {
privateKey, ok := os.LookupEnv(privateKeyEnv)
if !ok {
return nil, fmt.Errorf("private key environment variable %s empty", privateKeyEnv)
}
return New(tr, appID, installationID, []byte(privateKey))
}

// Client is a HTTP client which sends a http.Request and returns a http.Response
// or an error.
type Client interface {
Expand Down
11 changes: 11 additions & 0 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@ func TestNewKeyFromFile(t *testing.T) {
}
}

func TestNewKeyFromENV(t *testing.T) {
if err := os.Setenv("KEY", string(key)); err != nil {
t.Fatal("unexpected error:", err)
}

_, err := NewKeyFromEnv(&http.Transport{}, appID, installationID, "KEY")
if err != nil {
t.Fatal("unexpected error:", err)
}
}

func TestNew_appendHeader(t *testing.T) {
var headers http.Header
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down