Skip to content

Commit

Permalink
Issue #25, ReadMe and improved example for examples/sdkExample
Browse files Browse the repository at this point in the history
Signed-off-by: Phil Hunt <[email protected]>
  • Loading branch information
independentid committed Mar 27, 2024
1 parent 1a9ad86 commit 095799f
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 92 deletions.
20 changes: 20 additions & 0 deletions examples/sdkExample/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
![Hexa](https://hexaorchestration.org/wp-content/themes/hexa/img/logo.svg)

# Example: Using the SDK To Read Polcies from AVP

The GoLang code in [exampleIntegration.go](exampleIntegration.go) shows how to open an integration using the Hexa-Mapper SDK.
In this example, a credential file for AWS is read in and an integration is opened. The application then calls
`GetPolicyApplicationPoints` to discover the defined AVP integrations. It then calls `GetPolicies` and `SetPolicies` to retrieve
and set policies GoLang.

In this example, the retrieved policies are formatted in a hexaPolicy.Policies structure.

For more information on the contents of the Amazon credential file, use the Hexa CLI as follows:

```shell
hexa help add avp
```

See the AVP Provider [README](../../providers/aws/avpProvider/README.md) for more information.

See the [developer guide](../../docs/Developer.md) for more information on use of the SDK integration.
73 changes: 73 additions & 0 deletions examples/sdkExample/exampleIntegration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"encoding/json"
"fmt"
"os"
"strconv"

"github.com/hexa-org/policy-mapper/api/policyprovider"
"github.com/hexa-org/policy-mapper/pkg/hexapolicy"
"github.com/hexa-org/policy-mapper/sdk"
)

func main() {
keybytes, err := os.ReadFile("awsCredential.txt")
if err != nil {
panic(-1)
}

info := policyprovider.IntegrationInfo{
Name: sdk.ProviderTypeAvp,
Key: keybytes,
}

integration, err := sdk.OpenIntegration(sdk.WithIntegrationInfo(info))
if err != nil {
fmt.Println("Error opening integration: " + err.Error())
panic(-1)
}

apps, err := integration.GetPolicyApplicationPoints(nil)
if err != nil {
panic(-1)
}

for _, app := range apps {
jsonBytes, _ := json.MarshalIndent(app, "", " ")
fmt.Println(string(jsonBytes))
}

for alias := range integration.Apps {
policies := getAndPrintPolicies(integration, alias)

// Note that the returned policies object has the "app" alias included as policies.App.
setPolicies(integration, policies)
}

}

func getAndPrintPolicies(integration *sdk.Integration, alias string) *hexapolicy.Policies {

fmt.Println("PAP " + alias)

policies, err := integration.GetPolicies(alias)
if err != nil {
fmt.Println("Error retrieving policies: " + err.Error())
}
jsonBytes, _ := json.MarshalIndent(policies, "", " ")
fmt.Println("IDQL returned:")
fmt.Println(string(jsonBytes))

return policies
}

func setPolicies(integration *sdk.Integration, policies *hexapolicy.Policies) {

status, err := integration.SetPolicyInfo(*policies.App, policies.Policies)
if err != nil {
fmt.Println("Error getting policy: " + err.Error())
panic(-1)
}
fmt.Println("Request completed with http status " + strconv.Itoa(status))
}
29 changes: 0 additions & 29 deletions examples/sdkExample/getPolicies.go

This file was deleted.

42 changes: 0 additions & 42 deletions examples/sdkExample/openIntegration.go

This file was deleted.

21 changes: 0 additions & 21 deletions examples/sdkExample/setPolicies.go

This file was deleted.

0 comments on commit 095799f

Please sign in to comment.