Skip to content

Commit

Permalink
Merge pull request #9 from hugomcfonseca/fix-client-auth-using-terraf…
Browse files Browse the repository at this point in the history
…orm-auth

Fix OpenFaaS client authentication to use authentication from Terraform provider initialization
  • Loading branch information
ewilde authored Jun 4, 2020
2 parents 21a3939 + 0b5b5fc commit 3ea32c8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
26 changes: 23 additions & 3 deletions openfaas/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package openfaas

import (
"crypto/tls"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"log"
"net"
"net/http"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"

"github.com/openfaas/faas-cli/config"
"github.com/openfaas/faas-cli/proxy"
)
Expand Down Expand Up @@ -65,7 +66,9 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
log.Printf("[DEBUG] configuring provider")

gatewayURI := d.Get("uri").(string)
auth := newCLIAuth("", gatewayURI)
username := d.Get("user_name").(string)
password := d.Get("password").(string)
auth := newAuthChain(username, password, "", gatewayURI)
insecure := d.Get("tls_insecure").(bool)
transport := GetDefaultCLITransport(insecure, &defaultTimeout)
client := proxy.NewClient(auth, gatewayURI, transport, &defaultTimeout)
Expand All @@ -77,6 +80,21 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
return providerConfig, nil
}

func newAuthChain(username, password, token, gateway string) proxy.ClientAuth {
if username != "" && password != "" {
log.Print("[DEBUG] configuring basic-auth authentication from Terraform provider credentials")

return &BasicAuth{
username: username,
password: password,
}
}

log.Print("[DEBUG] empty Terraform provider credentials - falling back to OpenFaaS configuration file")
return newCLIAuth(token, gateway)

}

func newCLIAuth(token string, gateway string) proxy.ClientAuth {
authConfig, _ := config.LookupAuthConfig(gateway)

Expand All @@ -87,6 +105,7 @@ func newCLIAuth(token string, gateway string) proxy.ClientAuth {
)

if authConfig.Auth == config.BasicAuthType {
log.Printf("[DEBUG] configuring basic-auth authentication")
username, password, _ = config.DecodeAuth(authConfig.Token)

return &BasicAuth{
Expand All @@ -97,6 +116,7 @@ func newCLIAuth(token string, gateway string) proxy.ClientAuth {
}

// User specified token gets priority
log.Printf("[DEBUG] configuring token authentication")
if len(token) > 0 {
bearerToken = token
} else {
Expand Down
3 changes: 2 additions & 1 deletion openfaas/resource_openfaas_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,10 @@ func isFunctionNotFound(err error) bool {
var whiteListLabels = map[string]string{
"labels.com.openfaas.function": "",
"labels.function": "",
"labels.uid": "",
}

const extraProviderLabelsCount = 2
const extraProviderLabelsCount = 3

func labelsDiffFunc(k, old, new string, d *schema.ResourceData) bool {
if _, ok := whiteListLabels[k]; ok {
Expand Down
1 change: 1 addition & 0 deletions openfaas/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func expandDeploymentSpec(d *schema.ResourceData, name string) *proxy.DeployFunc
deploySpec := &proxy.DeployFunctionSpec{
FunctionName: name,
Image: d.Get("image").(string),
Update: true,
}

if v, ok := d.GetOk("network"); ok {
Expand Down

0 comments on commit 3ea32c8

Please sign in to comment.