Skip to content

Commit

Permalink
makeAuthenticatedReq to snap service
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorshephard committed Sep 26, 2023
1 parent 589fecc commit cdd2248
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
26 changes: 2 additions & 24 deletions cmd/smoke/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
"time"

signup "github.com/operationspark/service-signup"
"github.com/operationspark/service-signup/gcloud"
"github.com/operationspark/service-signup/greenlight"
"github.com/twilio/twilio-go"
twiAPI "github.com/twilio/twilio-go/rest/api/v2010"
"google.golang.org/api/idtoken"
)

type (
Expand Down Expand Up @@ -114,7 +114,7 @@ func (s *smoke) postSignup(su signup.Signup) error {
}

// Use Google Auth to trigger cloud function
req, err := makeAuthenticatedReq(http.MethodPost, s.signupAPIurl, &body)
req, err := gcloud.MakeAuthenticatedReq(context.Background(), http.MethodPost, s.signupAPIurl, &body)
if err != nil {
return fmt.Errorf("auth'd req: %w", err)
}
Expand All @@ -127,28 +127,6 @@ func (s *smoke) postSignup(su signup.Signup) error {
return checkHTTPError(resp)
}

// MakeAuthenticatedReq makes an HTTP request using Google Service Account credentials.
func makeAuthenticatedReq(method string, url string, body io.Reader) (*http.Request, error) {
audience := url
creds := os.Getenv("GCP_SA_CREDS_JSON")
opts := idtoken.WithCredentialsJSON([]byte(creds))

if creds == "" {
opts = idtoken.WithCredentialsFile("../../creds.json")
}
ts, err := idtoken.NewTokenSource(context.Background(), audience, opts)
if err != nil {
return nil, fmt.Errorf("newTokenSource: %w", err)
}
token, err := ts.Token()
if err != nil {
return nil, fmt.Errorf("token: %w", err)
}
req, err := http.NewRequest(method, audience, body)
token.SetAuthHeader(req)
return req, err
}

func fetchLastTextMessages(toNum, fromNum string, n int) ([]string, error) {
accountSID := os.Getenv("TWILIO_ACCOUNT_SID")

Expand Down
34 changes: 34 additions & 0 deletions gcloud/gcloud.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package gcloud

import (
"context"
"fmt"
"io"
"net/http"
"os"

"google.golang.org/api/idtoken"
)

// MakeAuthenticatedReq makes an HTTP request using Google Service Account credentials.
// https://cloud.google.com/run/docs/authenticating/service-to-service#acquire-token
func MakeAuthenticatedReq(ctx context.Context, method string, url string, body io.Reader) (*http.Request, error) {
audience := url
creds := os.Getenv("GCP_SA_CREDS_JSON")
opts := idtoken.WithCredentialsJSON([]byte(creds))

if creds == "" {
opts = idtoken.WithCredentialsFile("../creds.json")
}
ts, err := idtoken.NewTokenSource(ctx, audience, opts)
if err != nil {
return nil, fmt.Errorf("newTokenSource: %w", err)
}
token, err := ts.Token()
if err != nil {
return nil, fmt.Errorf("token: %w", err)
}
req, err := http.NewRequestWithContext(ctx, method, audience, body)
token.SetAuthHeader(req)
return req, err
}
4 changes: 3 additions & 1 deletion snapmail.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"encoding/json"
"net/http"
"time"

"github.com/operationspark/service-signup/gcloud"
)

type SnapMail struct {
Expand Down Expand Up @@ -54,7 +56,7 @@ func (sm *SnapMail) run(ctx context.Context, signup Signup) error {
return err
}

req, err := http.NewRequestWithContext(ctx, http.MethodPost, sm.url, bytes.NewReader(payload))
req, err := gcloud.MakeAuthenticatedReq(ctx, http.MethodPost, sm.url, bytes.NewReader(payload))
if err != nil {
return err
}
Expand Down

0 comments on commit cdd2248

Please sign in to comment.