github.com/newrelic/newrelic-client-go@v1.1.0/internal/http/auth.go (about)

     1  package http
     2  
     3  import (
     4  	"github.com/newrelic/newrelic-client-go/pkg/config"
     5  	"github.com/newrelic/newrelic-client-go/pkg/contextkeys"
     6  )
     7  
     8  // RequestAuthorizer is an interface that allows customizatino of how a request is authorized.
     9  type RequestAuthorizer interface {
    10  	AuthorizeRequest(r *Request, c *config.Config)
    11  }
    12  
    13  // NerdGraphAuthorizer authorizes calls to NerdGraph.
    14  type NerdGraphAuthorizer struct{}
    15  
    16  // AuthorizeRequest is responsible for setting up auth for a request.
    17  func (a *NerdGraphAuthorizer) AuthorizeRequest(r *Request, c *config.Config) {
    18  	r.SetHeader("Api-Key", c.PersonalAPIKey)
    19  }
    20  
    21  // PersonalAPIKeyCapableV2Authorizer authorizes V2 endpoints that can use a personal API key.
    22  type PersonalAPIKeyCapableV2Authorizer struct{}
    23  
    24  // AuthorizeRequest is responsible for setting up auth for a request.
    25  func (a *PersonalAPIKeyCapableV2Authorizer) AuthorizeRequest(r *Request, c *config.Config) {
    26  	if c.PersonalAPIKey != "" {
    27  		r.SetHeader("Api-Key", c.PersonalAPIKey)
    28  	} else {
    29  		r.SetHeader("X-Api-Key", c.AdminAPIKey)
    30  	}
    31  
    32  	if accountID, ok := contextkeys.GetAccountID(r.request.Context()); ok {
    33  		r.SetHeader("X-Account-ID", accountID)
    34  	}
    35  }
    36  
    37  // ClassicV2Authorizer authorizes V2 endpoints that cannot use a personal API key.
    38  type ClassicV2Authorizer struct{}
    39  
    40  // AuthorizeRequest is responsible for setting up auth for a request.
    41  func (a *ClassicV2Authorizer) AuthorizeRequest(r *Request, c *config.Config) {
    42  	r.SetHeader("X-Api-Key", c.AdminAPIKey)
    43  }
    44  
    45  // InsightsInsertKeyAuthorizer authorizes sending custom events to New Relic.
    46  type InsightsInsertKeyAuthorizer struct{}
    47  
    48  func (a *InsightsInsertKeyAuthorizer) AuthorizeRequest(r *Request, c *config.Config) {
    49  	r.SetHeader("X-Insert-Key", c.InsightsInsertKey)
    50  }
    51  
    52  type LicenseKeyAuthorizer struct{}
    53  
    54  func (a *LicenseKeyAuthorizer) AuthorizeRequest(r *Request, c *config.Config) {
    55  	r.SetHeader("X-License-Key", c.LicenseKey)
    56  }
    57  
    58  type LogsInsertKeyAuthorizer struct{}
    59  
    60  func (a *LogsInsertKeyAuthorizer) AuthorizeRequest(r *Request, c *config.Config) {
    61  	r.SetHeader("Api-Key", c.InsightsInsertKey)
    62  }