Skip to content

Commit

Permalink
Merge pull request #1155 from Tharsanan1/send-scopes
Browse files Browse the repository at this point in the history
Fix openapi
  • Loading branch information
CrowleyRajapakse authored Apr 9, 2024
2 parents 13aa308 + 449567b commit 09b59f2
Showing 1 changed file with 50 additions and 28 deletions.
78 changes: 50 additions & 28 deletions apim-apk-agent/pkg/managementserver/rest_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ func StartInternalServer(port uint) {
if strings.EqualFold(event.API.APIType, "rest") && event.API.Definition == "" {
event.API.Definition = utils.OpenAPIDefaultYaml
}
if strings.EqualFold(event.API.APIType, "rest") {
yaml, errJSONToYaml := JSONToYAML(event.API.Definition)
if errJSONToYaml == nil {
event.API.Definition = yaml
}
}
apiYaml, definition := createAPIYaml(&event)
deploymentContent := createDeployementYaml(event.API.Vhost)
logger.LoggerMgtServer.Debugf("Created apiYaml : %s, \n\n\n created definition file: %s", apiYaml, definition)
Expand Down Expand Up @@ -221,16 +227,17 @@ func createAPIYaml(apiCPEvent *APICPEvent) (string, string) {
for verb, verbContent := range pathContentMap {
for _, operation := range operations {
if strings.EqualFold(path.(string), operation.Target) && strings.EqualFold(verb.(string), operation.Verb) {
if len(operation.Scopes) > 0 {
if verbContentMap, ok := verbContent.(map[interface{}]interface{}); ok {
if verbContentMap, ok := verbContent.(map[interface{}]interface{}); ok {
if len(operation.Scopes) > 0 {
verbContentMap["security"] = []map[string][]string{
{
"default": operation.Scopes,
},
}
verbContentMap["x-auth-type"] = "Application & Application User"
}
verbContentMap["x-auth-type"] = "Application & Application User"
}
break
}
}
}
Expand All @@ -243,24 +250,24 @@ func createAPIYaml(apiCPEvent *APICPEvent) (string, string) {
}

components, ok := openAPI["components"].(map[interface{}]interface{})
if !ok {
components = make(map[interface{}]interface{})
}
securitySchemes, ok := components["securitySchemes"].(map[interface{}]interface{})
if !ok {
securitySchemes = make(map[interface{}]interface{})
}
if !ok {
components = make(map[interface{}]interface{})
}
securitySchemes, ok := components["securitySchemes"].(map[interface{}]interface{})
if !ok {
securitySchemes = make(map[interface{}]interface{})
}

securitySchemes["default"] = map[interface{}]interface{}{
"type": "oauth2",
"flows": map[interface{}]interface{}{
"implicit": map[interface{}]interface{}{
"authorizationUrl": "https://test.com",
"scopes": scopesForOpenAPIComponents,
"type": "oauth2",
"flows": map[interface{}]interface{}{
"implicit": map[interface{}]interface{}{
"authorizationUrl": "https://test.com",
"scopes": scopesForOpenAPIComponents,
"x-scopes-bindings": scopesForOpenAPIComponents,
},
},
}
},
},
}

components["securitySchemes"] = securitySchemes
openAPI["components"] = components
Expand Down Expand Up @@ -321,7 +328,7 @@ type APIOperation struct {

// OpenAPIPaths represents the structure of the OpenAPI specification YAML file
type OpenAPIPaths struct {
Paths map[string]map[string]Operation `yaml:"paths"`
Paths map[string]map[string]interface{} `yaml:"paths"`
}

// Operation represents the structure of an operation within the OpenAPI specification
Expand Down Expand Up @@ -376,13 +383,7 @@ func extractOperations(event APICPEvent) ([]APIOperation, []ScopeWrapper, error)
}

for path, operations := range openAPIPaths.Paths {
for verb, operation := range operations {
if operation.XAuthType == "" {
operation.XAuthType = "Application & Application User"
}
if operation.XThrottlingTier == "" {
operation.XThrottlingTier = "Unlimited"
}
for verb := range operations {
ptrToOperationFromDP := findMatchingAPKOperation(path, verb, event.API.Operations)
if ptrToOperationFromDP == nil {
continue
Expand All @@ -403,8 +404,8 @@ func extractOperations(event APICPEvent) ([]APIOperation, []ScopeWrapper, error)
apiOp := APIOperation{
Target: path,
Verb: verb,
AuthType: operation.XAuthType,
ThrottlingPolicy: operation.XThrottlingTier,
AuthType: "Application & Application User",
ThrottlingPolicy: "Unlimited",
Scopes: scopes,
}
apiOperations = append(apiOperations, apiOp)
Expand Down Expand Up @@ -476,3 +477,24 @@ func ConvertYAMLToMap(yamlString string) (map[string]interface{}, error) {
}
return yamlData, nil
}

// JSONToYAML convert json string to yaml
func JSONToYAML(jsonString string) (string, error) {
// Convert JSON string to map[string]interface{}
var jsonData map[string]interface{}
err := json.Unmarshal([]byte(jsonString), &jsonData)
if err != nil {
return "", err
}

// Convert map[string]interface{} to YAML
yamlBytes, err := yaml.Marshal(jsonData)
if err != nil {
return "", err
}

// Convert YAML bytes to string
yamlString := string(yamlBytes)

return yamlString, nil
}

0 comments on commit 09b59f2

Please sign in to comment.