@@ -16,6 +16,7 @@ import (
1616 "log/slog"
1717 "net"
1818 "net/http"
19+ "net/url"
1920 "os"
2021 "path/filepath"
2122 "regexp"
@@ -269,18 +270,20 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
269270 return nginxConfigContext , fmt .Errorf ("traverse nginx config: %w" , err )
270271 }
271272
272- stubStatuses := ncp .crossplaneConfigTraverseAPIDetails (
273- ctx , & conf , ncp .apiCallback , stubStatusAPIDirective ,
274- )
275- if stubStatuses != nil {
276- nginxConfigContext .StubStatuses = append (nginxConfigContext .StubStatuses , stubStatuses ... )
277- }
273+ if ! ncp .agentConfig .IsNginxApiUrlConfigured () {
274+ stubStatuses := ncp .crossplaneConfigTraverseAPIDetails (
275+ ctx , & conf , ncp .apiCallback , stubStatusAPIDirective ,
276+ )
277+ if stubStatuses != nil {
278+ nginxConfigContext .StubStatuses = append (nginxConfigContext .StubStatuses , stubStatuses ... )
279+ }
278280
279- plusAPIs := ncp .crossplaneConfigTraverseAPIDetails (
280- ctx , & conf , ncp .apiCallback , plusAPIDirective ,
281- )
282- if plusAPIs != nil {
283- nginxConfigContext .PlusAPIs = append (nginxConfigContext .PlusAPIs , plusAPIs ... )
281+ plusAPIs := ncp .crossplaneConfigTraverseAPIDetails (
282+ ctx , & conf , ncp .apiCallback , plusAPIDirective ,
283+ )
284+ if plusAPIs != nil {
285+ nginxConfigContext .PlusAPIs = append (nginxConfigContext .PlusAPIs , plusAPIs ... )
286+ }
284287 }
285288
286289 fileMeta , err := files .FileMeta (conf .File )
@@ -300,13 +303,48 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
300303 "server configured on port %s" , ncp .agentConfig .SyslogServer .Port ))
301304 }
302305
303- nginxConfigContext .PlusAPIs = ncp .sortPlusAPIs (ctx , nginxConfigContext .PlusAPIs )
304- nginxConfigContext .StubStatus = ncp .FindStubStatusAPI (ctx , nginxConfigContext )
305- nginxConfigContext .PlusAPI = ncp .FindPlusAPI (ctx , nginxConfigContext )
306+ if ! ncp .agentConfig .IsNginxApiUrlConfigured () {
307+ nginxConfigContext .PlusAPIs = ncp .sortPlusAPIs (ctx , nginxConfigContext .PlusAPIs )
308+ nginxConfigContext .StubStatus = ncp .FindStubStatusAPI (ctx , nginxConfigContext )
309+ nginxConfigContext .PlusAPI = ncp .FindPlusAPI (ctx , nginxConfigContext )
310+ } else {
311+ nginxConfigContext = ncp .addApiToNginxConfigContext (ctx , nginxConfigContext )
312+ }
306313
307314 return nginxConfigContext , nil
308315}
309316
317+ func (ncp * NginxConfigParser ) addApiToNginxConfigContext (
318+ ctx context.Context ,
319+ nginxConfigContext * model.NginxConfigContext ,
320+ ) * model.NginxConfigContext {
321+ apiDetails , err := parseURL (ncp .agentConfig .DataPlaneConfig .Nginx .API .URL )
322+ if err != nil {
323+ slog .ErrorContext (
324+ ctx ,
325+ "Configured NGINX API URL is invalid" ,
326+ "url" , ncp .agentConfig .DataPlaneConfig .Nginx .API .URL ,
327+ "error" , err ,
328+ )
329+
330+ return nginxConfigContext
331+ }
332+
333+ if ncp .pingAPIEndpoint (ctx , apiDetails , stubStatusAPIDirective ) {
334+ nginxConfigContext .StubStatus = apiDetails
335+ } else if ncp .pingAPIEndpoint (ctx , apiDetails , plusAPIDirective ) {
336+ nginxConfigContext .PlusAPI = apiDetails
337+ } else {
338+ slog .WarnContext (
339+ ctx ,
340+ "Configured NGINX API URL is not reachable" ,
341+ "url" , ncp .agentConfig .DataPlaneConfig .Nginx .API .URL ,
342+ )
343+ }
344+
345+ return nginxConfigContext
346+ }
347+
310348func (ncp * NginxConfigParser ) findLocalSysLogServers (sysLogServer string ) string {
311349 re := regexp .MustCompile (`syslog:server=([\S]+)` )
312350 matches := re .FindStringSubmatch (sysLogServer )
@@ -886,24 +924,26 @@ func (ncp *NginxConfigParser) socketClient(socketPath string) *http.Client {
886924// prepareHTTPClient handles TLS config
887925func (ncp * NginxConfigParser ) prepareHTTPClient (ctx context.Context ) (* http.Client , error ) {
888926 httpClient := http .DefaultClient
889- caCertLocation := ncp .agentConfig .DataPlaneConfig .Nginx .APITls .Ca
890-
891- if caCertLocation != "" && ncp .agentConfig .IsDirectoryAllowed (caCertLocation ) {
892- slog .DebugContext (ctx , "Reading CA certificate" , "file_path" , caCertLocation )
893- caCert , err := os .ReadFile (caCertLocation )
894- if err != nil {
895- return nil , err
896- }
897- caCertPool := x509 .NewCertPool ()
898- caCertPool .AppendCertsFromPEM (caCert )
899-
900- httpClient = & http.Client {
901- Transport : & http.Transport {
902- TLSClientConfig : & tls.Config {
903- RootCAs : caCertPool ,
904- MinVersion : tls .VersionTLS13 ,
927+ if ncp .agentConfig .IsNginxApiConfigured () {
928+ caCertLocation := ncp .agentConfig .DataPlaneConfig .Nginx .API .TLS .Ca
929+
930+ if caCertLocation != "" && ncp .agentConfig .IsDirectoryAllowed (caCertLocation ) {
931+ slog .DebugContext (ctx , "Reading CA certificate" , "file_path" , caCertLocation )
932+ caCert , err := os .ReadFile (caCertLocation )
933+ if err != nil {
934+ return nil , err
935+ }
936+ caCertPool := x509 .NewCertPool ()
937+ caCertPool .AppendCertsFromPEM (caCert )
938+
939+ httpClient = & http.Client {
940+ Transport : & http.Transport {
941+ TLSClientConfig : & tls.Config {
942+ RootCAs : caCertPool ,
943+ MinVersion : tls .VersionTLS13 ,
944+ },
905945 },
906- },
946+ }
907947 }
908948 }
909949
@@ -912,15 +952,19 @@ func (ncp *NginxConfigParser) prepareHTTPClient(ctx context.Context) (*http.Clie
912952
913953// Populate the CA cert location based ondirectory allowance.
914954func (ncp * NginxConfigParser ) selfSignedCACertLocation (ctx context.Context ) string {
915- caCertLocation := ncp .agentConfig .DataPlaneConfig .Nginx .APITls .Ca
955+ if ncp .agentConfig .IsNginxApiConfigured () {
956+ caCertLocation := ncp .agentConfig .DataPlaneConfig .Nginx .API .TLS .Ca
957+
958+ if caCertLocation != "" && ! ncp .agentConfig .IsDirectoryAllowed (caCertLocation ) {
959+ // If SSL is enabled but CA cert is provided and not allowed, treat it as if no CA cert
960+ slog .WarnContext (ctx , "CA certificate location is not allowed, treating as if no CA cert provided." )
961+ return ""
962+ }
916963
917- if caCertLocation != "" && ! ncp .agentConfig .IsDirectoryAllowed (caCertLocation ) {
918- // If SSL is enabled but CA cert is provided and not allowed, treat it as if no CA cert
919- slog .WarnContext (ctx , "CA certificate location is not allowed, treating as if no CA cert provided." )
920- return ""
964+ return caCertLocation
921965 }
922966
923- return caCertLocation
967+ return ""
924968}
925969
926970func (ncp * NginxConfigParser ) isDuplicateFile (nginxConfigContextFiles []* mpi.File , newFile * mpi.File ) bool {
@@ -976,3 +1020,16 @@ func (ncp *NginxConfigParser) sortPlusAPIs(ctx context.Context, apis []*model.AP
9761020
9771021 return apis
9781022}
1023+
1024+ func parseURL (unparsedUrl string ) (* model.APIDetails , error ) {
1025+ parsedURL , err := url .Parse (unparsedUrl )
1026+ if err != nil {
1027+ return nil , err
1028+ }
1029+
1030+ return & model.APIDetails {
1031+ URL : unparsedUrl ,
1032+ Listen : parsedURL .Host ,
1033+ Location : parsedURL .Path ,
1034+ }, nil
1035+ }
0 commit comments