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

Support remote auth for policy resources #302

Merged
merged 2 commits into from
Apr 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var defaultRetryOnStatusCodes = []int{429, 503}
var toleratePartialSuccess = false
var policyEnforcementPoint = "default"
var policySite = "default"
var policyRemoteAuth = false

type nsxtClients struct {
// NSX Manager client - based on go-vmware-nsxt SDK
Expand Down Expand Up @@ -439,6 +440,7 @@ func configurePolicyConnectorData(d *schema.ResourceData, clients *nsxtClients)
clientAuthKeyFile := d.Get("client_auth_key_file").(string)
caFile := d.Get("ca_file").(string)
policyEnforcementPoint = d.Get("enforcement_point").(string)
policyRemoteAuth = d.Get("remote_auth").(bool)

if hostIP == "" {
return fmt.Errorf("host must be provided")
Expand All @@ -447,7 +449,7 @@ func configurePolicyConnectorData(d *schema.ResourceData, clients *nsxtClients)
host := fmt.Sprintf("https://%s", hostIP)
securityCtx := core.NewSecurityContextImpl()
securityContextNeeded := true
if len(clientAuthCertFile) > 0 {
if len(clientAuthCertFile) > 0 && !policyRemoteAuth {
securityContextNeeded = false
}

Expand Down Expand Up @@ -499,6 +501,20 @@ func configurePolicyConnectorData(d *schema.ResourceData, clients *nsxtClients)
return nil
}

type remoteBasicAuthHeaderProcessor struct {
}

func newRemoteBasicAuthHeaderProcessor() *remoteBasicAuthHeaderProcessor {
return &remoteBasicAuthHeaderProcessor{}
}

func (processor remoteBasicAuthHeaderProcessor) Process(req *http.Request) error {
oldAuthHeader := req.Header.Get("Authorization")
newAuthHeader := strings.Replace(oldAuthHeader, "Basic", "Remote", 1)
req.Header.Set("Authorization", newAuthHeader)
return nil
}

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
nsxtClient, err := configureNsxtClient(d)
if err != nil {
Expand All @@ -523,5 +539,9 @@ func getPolicyConnector(clients interface{}) *client.RestConnector {
if c.PolicySecurityContext != nil {
connector.SetSecurityContext(c.PolicySecurityContext)
}
if policyRemoteAuth {
connector.AddRequestProcessor(newRemoteBasicAuthHeaderProcessor())
}

return connector
}