Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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
18 changes: 14 additions & 4 deletions api/grpc/mpi/v1/command.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/grpc/mpi/v1/command.pb.validate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/grpc/mpi/v1/command.proto
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ message APIDetails {
string listen = 2;
// the API CA file path
string Ca = 3;
// flag to know API is configured with 'write=on;'
bool write_enabled = 4;
}

// A set of runtime NGINX App Protect settings
Expand Down
2 changes: 1 addition & 1 deletion api/grpc/mpi/v1/common.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/grpc/mpi/v1/files.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/proto/protos.md
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@ Perform an associated API action on an instance
| location | [string](#string) | | the API location directive |
| listen | [string](#string) | | the API listen directive |
| Ca | [string](#string) | | the API CA file path |
| write_enabled | [bool](#bool) | | flag to know API is configured with 'write=on;' |



Expand Down
75 changes: 62 additions & 13 deletions internal/datasource/config/nginx_config_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ type (
current *crossplane.Directive, apiType string) []*model.APIDetails
)

type createAPIDetailsParams struct {
locationDirectiveName string
address string
path string
caCertLocation string
isSSL bool
writeEnabled bool
}

func NewNginxConfigParser(agentConfig *config.Config) *NginxConfigParser {
return &NginxConfigParser{
agentConfig: agentConfig,
Expand Down Expand Up @@ -251,6 +260,8 @@ func (ncp *NginxConfigParser) createNginxConfigContext(
nginxConfigContext.PlusAPIs = append(nginxConfigContext.PlusAPIs, plusAPIs...)
}

nginxConfigContext.PlusAPIs = ncp.sortPlusAPIs(nginxConfigContext.PlusAPIs)

if len(napSyslogServersFound) > 0 {
var napSyslogServer []string
for server := range napSyslogServersFound {
Expand Down Expand Up @@ -674,6 +685,7 @@ func validateAPIResponse(apiType string, bodyBytes []byte) error {
return nil
}

//nolint:revive //need to reduce cognitive complexity
func (ncp *NginxConfigParser) apiDetailsFromLocationDirective(
ctx context.Context, parent, current *crossplane.Directive,
locationDirectiveName string,
Expand All @@ -699,11 +711,30 @@ func (ncp *NginxConfigParser) apiDetailsFromLocationDirective(
addresses := ncp.parseAddressFromServerDirective(parent)
path := ncp.parsePathFromLocationDirective(current)

writeEnabled := false
if locChild.Directive == plusAPIDirective {
for _, arg := range locChild.Args {
if strings.EqualFold(arg, "write=on") {
writeEnabled = true
break
}
}
}

if locChild.Directive == locationDirectiveName {
for _, address := range addresses {
params := createAPIDetailsParams{
locationDirectiveName: locationDirectiveName,
address: address,
path: path,
caCertLocation: caCertLocation,
isSSL: isSSL,
writeEnabled: writeEnabled,
}

details = append(
details,
ncp.createAPIDetails(locationDirectiveName, address, path, caCertLocation, isSSL),
ncp.createAPIDetails(params),
)
}
}
Expand All @@ -713,28 +744,30 @@ func (ncp *NginxConfigParser) apiDetailsFromLocationDirective(
}

func (ncp *NginxConfigParser) createAPIDetails(
locationDirectiveName, address, path, caCertLocation string, isSSL bool,
params createAPIDetailsParams,
) (details *model.APIDetails) {
if strings.HasPrefix(address, "unix:") {
if strings.HasPrefix(params.address, "unix:") {
format := unixStubStatusFormat

if locationDirectiveName == plusAPIDirective {
if params.locationDirectiveName == plusAPIDirective {
format = unixPlusAPIFormat
}

details = &model.APIDetails{
URL: fmt.Sprintf(format, path),
Listen: address,
Location: path,
Ca: caCertLocation,
URL: fmt.Sprintf(format, params.path),
Listen: params.address,
Location: params.path,
Ca: params.caCertLocation,
WriteEnabled: params.writeEnabled,
}
} else {
details = &model.APIDetails{
URL: fmt.Sprintf("%s://%s%s", map[bool]string{true: "https", false: "http"}[isSSL],
address, path),
Listen: address,
Location: path,
Ca: caCertLocation,
URL: fmt.Sprintf("%s://%s%s", map[bool]string{true: "https", false: "http"}[params.isSSL],
params.address, params.path),
Listen: params.address,
Location: params.path,
Ca: params.caCertLocation,
WriteEnabled: params.writeEnabled,
}
}

Expand Down Expand Up @@ -888,3 +921,19 @@ func (ncp *NginxConfigParser) isDuplicateFile(nginxConfigContextFiles []*mpi.Fil

return false
}

func (ncp *NginxConfigParser) sortPlusAPIs(apis []*model.APIDetails) []*model.APIDetails {
slices.SortFunc(apis, func(a, b *model.APIDetails) int {
if a.WriteEnabled && !b.WriteEnabled {
return -1
}

if b.WriteEnabled && !a.WriteEnabled {
return 1
}

return 0
})

return apis
}
Loading
Loading