Skip to content
Open
Show file tree
Hide file tree
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
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does a change need to happen to the MPI ? Do the management planes care about the write status ? Could the change instead only be made to the APIDetails struct thats internal to the Agent?

}

// 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
80 changes: 67 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 @@ -699,11 +710,22 @@ func (ncp *NginxConfigParser) apiDetailsFromLocationDirective(
addresses := ncp.parseAddressFromServerDirective(parent)
path := ncp.parsePathFromLocationDirective(current)

writeEnabled := ncp.isWriteEnabled(locChild)

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 +735,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 +912,33 @@ func (ncp *NginxConfigParser) isDuplicateFile(nginxConfigContextFiles []*mpi.Fil

return false
}

func (ncp *NginxConfigParser) isWriteEnabled(locChild *crossplane.Directive) bool {
if locChild.Directive != plusAPIDirective {
return false
}

for _, arg := range locChild.Args {
if strings.EqualFold(arg, "write=on") {
return true
}
}

return false
}

func (ncp *NginxConfigParser) sortPlusAPIs(apis []*model.APIDetails) []*model.APIDetails {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment or update the naming to explain that this is trying to prioritise and find apis with write enabled?

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