@@ -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,52 @@ 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 .agentConfig .IsNginxApiSocketConfigured () {
334+ apiDetails .Listen = ncp .agentConfig .DataPlaneConfig .Nginx .API .Socket
335+ }
336+
337+ if ncp .pingAPIEndpoint (ctx , apiDetails , stubStatusAPIDirective ) {
338+ nginxConfigContext .StubStatus = apiDetails
339+ } else if ncp .pingAPIEndpoint (ctx , apiDetails , plusAPIDirective ) {
340+ nginxConfigContext .PlusAPI = apiDetails
341+ } else {
342+ slog .WarnContext (
343+ ctx ,
344+ "Configured NGINX API URL is not reachable" ,
345+ "url" , ncp .agentConfig .DataPlaneConfig .Nginx .API .URL ,
346+ )
347+ }
348+
349+ return nginxConfigContext
350+ }
351+
310352func (ncp * NginxConfigParser ) findLocalSysLogServers (sysLogServer string ) string {
311353 re := regexp .MustCompile (`syslog:server=([\S]+)` )
312354 matches := re .FindStringSubmatch (sysLogServer )
@@ -886,24 +928,26 @@ func (ncp *NginxConfigParser) socketClient(socketPath string) *http.Client {
886928// prepareHTTPClient handles TLS config
887929func (ncp * NginxConfigParser ) prepareHTTPClient (ctx context.Context ) (* http.Client , error ) {
888930 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 ,
931+ if ncp .agentConfig .IsNginxApiConfigured () {
932+ caCertLocation := ncp .agentConfig .DataPlaneConfig .Nginx .API .TLS .Ca
933+
934+ if caCertLocation != "" && ncp .agentConfig .IsDirectoryAllowed (caCertLocation ) {
935+ slog .DebugContext (ctx , "Reading CA certificate" , "file_path" , caCertLocation )
936+ caCert , err := os .ReadFile (caCertLocation )
937+ if err != nil {
938+ return nil , err
939+ }
940+ caCertPool := x509 .NewCertPool ()
941+ caCertPool .AppendCertsFromPEM (caCert )
942+
943+ httpClient = & http.Client {
944+ Transport : & http.Transport {
945+ TLSClientConfig : & tls.Config {
946+ RootCAs : caCertPool ,
947+ MinVersion : tls .VersionTLS13 ,
948+ },
905949 },
906- },
950+ }
907951 }
908952 }
909953
@@ -912,15 +956,19 @@ func (ncp *NginxConfigParser) prepareHTTPClient(ctx context.Context) (*http.Clie
912956
913957// Populate the CA cert location based ondirectory allowance.
914958func (ncp * NginxConfigParser ) selfSignedCACertLocation (ctx context.Context ) string {
915- caCertLocation := ncp .agentConfig .DataPlaneConfig .Nginx .APITls .Ca
959+ if ncp .agentConfig .IsNginxApiConfigured () {
960+ caCertLocation := ncp .agentConfig .DataPlaneConfig .Nginx .API .TLS .Ca
916961
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 ""
962+ if caCertLocation != "" && ! ncp .agentConfig .IsDirectoryAllowed (caCertLocation ) {
963+ // If SSL is enabled but CA cert is provided and not allowed, treat it as if no CA cert
964+ slog .WarnContext (ctx , "CA certificate location is not allowed, treating as if no CA cert provided." )
965+ return ""
966+ }
967+
968+ return caCertLocation
921969 }
922970
923- return caCertLocation
971+ return ""
924972}
925973
926974func (ncp * NginxConfigParser ) isDuplicateFile (nginxConfigContextFiles []* mpi.File , newFile * mpi.File ) bool {
@@ -976,3 +1024,16 @@ func (ncp *NginxConfigParser) sortPlusAPIs(ctx context.Context, apis []*model.AP
9761024
9771025 return apis
9781026}
1027+
1028+ func parseURL (unparsedUrl string ) (* model.APIDetails , error ) {
1029+ parsedURL , err := url .Parse (unparsedUrl )
1030+ if err != nil {
1031+ return nil , err
1032+ }
1033+
1034+ return & model.APIDetails {
1035+ URL : unparsedUrl ,
1036+ Listen : parsedURL .Host ,
1037+ Location : parsedURL .Path ,
1038+ }, nil
1039+ }
0 commit comments