Skip to content

Commit

Permalink
removed incorrect function
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Theuermann <[email protected]>
  • Loading branch information
mati007thm committed Dec 3, 2024
1 parent 2a5f48f commit 9c136f2
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 30 deletions.
5 changes: 3 additions & 2 deletions examples/resources/mondoo_exception/resource.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
provider "mondoo" {
space = "eu-heuristic-hawking-332652"
space = "eu-practical-goldwasser-115737"
}

resource "mondoo_exception" "exception" {
scope_mrn = "//assets.api.mondoo.app/spaces/eu-practical-goldwasser-115737/assets/2phR0MlEtyxv1kknyk7nNXYjddW"
# valid_until = "2024-12-03"
justification = "This is a test exception"
action ="DISABLE"
# check_mrns = ["//policy.api.mondoo.app/queries/mondoo-http-security-x-content-type-options-nosniff"]
check_mrns = ["//policy.api.mondoo.app/queries/mondoo-http-security-content-security-policy"]
check_mrns = ["//policy.api.mondoo.app/queries/mondoo-http-security-x-content-type-options-nosniff"]
}
18 changes: 7 additions & 11 deletions internal/provider/exception_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,6 @@ func (r *exceptionResource) Create(ctx context.Context, req resource.CreateReque
return
}

// Compute and validate the space
scope, err := r.client.ComputeSpace(data.ScopeMrn)
if err != nil {
resp.Diagnostics.AddError("Invalid Configuration", err.Error())
return
}
ctx = tflog.SetField(ctx, "scope_mrn", scope.MRN())

checks := []string{}
data.CheckMrns.ElementsAs(ctx, &checks, false)

Expand All @@ -153,14 +145,18 @@ func (r *exceptionResource) Create(ctx context.Context, req resource.CreateReque
}

// Create API call logic
tflog.Debug(ctx, fmt.Sprintf("Creating exception for scope %s", scope.MRN()))
err = r.client.ApplyException(ctx, scope.MRN(), mondoov1.ExceptionMutationAction(data.Action.ValueString()), checks, []string{}, []string{}, vulnerabilities, data.Justification.ValueStringPointer(), &validUntilStr, (*bool)(mondoov1.NewBooleanPtr(false)))
// mondoov1.ExceptionMutationAction(data.Action.ValueString())
tflog.Debug(ctx, fmt.Sprintf("Creating exception for scope %s", data.ScopeMrn.ValueString()))
err := r.client.ApplyException(ctx, data.ScopeMrn.ValueString(), mondoov1.ExceptionMutationAction(data.Action.ValueString()), checks, []string{}, []string{}, vulnerabilities, data.Justification.ValueStringPointer(), &validUntilStr, (*bool)(mondoov1.NewBooleanPtr(false)))
fmt.Println("====================================")
fmt.Println("Error:", err)
fmt.Println("====================================")
if err != nil {
resp.Diagnostics.AddError("Failed to create exception", err.Error())
return
}

data.ScopeMrn = types.StringValue(scope.MRN())
data.ScopeMrn = types.StringValue(data.ScopeMrn.ValueString())
data.ValidUntil = types.StringValue(validUntilStr)

// Save data into Terraform state
Expand Down
116 changes: 99 additions & 17 deletions internal/provider/gql.go
Original file line number Diff line number Diff line change
Expand Up @@ -962,33 +962,115 @@ func (c *ExtendedGqlClient) ApplyException(

// Prepare input fields
input := mondoov1.ExceptionMutationInput{
ScopeMrn: mondoov1.String(scopeMrn),
Action: action,
QueryMrns: convertToGraphQLList(checkMrns),
ControlMrns: convertToGraphQLList(controlMrns),
CveMrns: convertToGraphQLList(cveMrns),
ScopeMrn: mondoov1.String(scopeMrn),
Action: action,
QueryMrns: convertToGraphQLList(checkMrns),
// ControlMrns: convertToGraphQLList(controlMrns),
// CveMrns: convertToGraphQLList(cveMrns),
AdvisoryMrns: convertToGraphQLList(vulnerabilityMrns),
Justification: (*mondoov1.String)(justification),
ValidUntil: (*mondoov1.String)(validUntil),
ApplyToCves: mondoov1.NewBooleanPtr(mondoov1.Boolean(*applyToCves)),
}
// ValidUntil: (*mondoov1.String)(validUntil),
ApplyToCves: mondoov1.NewBooleanPtr(mondoov1.Boolean(*applyToCves)),
}
fmt.Println("====================================")
fmt.Println("Scope:", input.ScopeMrn)
fmt.Println("Action:", input.Action)
fmt.Println("CheckMrns:", *input.QueryMrns)
// fmt.Println("ControlMrns:", *input.ControlMrns)
// fmt.Println("CveMrns:", *input.CveMrns)
// fmt.Println("VulnerabilityMrns:", *input.AdvisoryMrns)
// fmt.Println("Justification:", *input.Justification)
// fmt.Println("ValidUntil:", *input.ValidUntil)
fmt.Println("ApplyToCves:", *input.ApplyToCves)
fmt.Println("====================================")

return c.Mutate(ctx, &applyException, input, nil)
}

// ListExceptionGroup
func (c *ExtendedGqlClient) ListExceptionGroup(ctx context.Context, scopeMrn string) ([]mondoov1.ExceptionGroupsInput, error) {
var q struct {
ExceptionGroups []mondoov1.ExceptionGroupsInput `graphql:"exceptionGroups(scopeMrn: $scopeMrn)"`
// ListExceptionGroupsInput defines the input for the ListExceptionGroups query

Check failure on line 990 in internal/provider/gql.go

View workflow job for this annotation

GitHub Actions / Build

Comment should end in a period (godot)
type ListExceptionGroupsInput struct {
ScopeMrn string `json:"scopeMrn"`
Types []string `json:"types"`
}

// Reviewer represents the reviewer of the exception group

Check failure on line 996 in internal/provider/gql.go

View workflow job for this annotation

GitHub Actions / Build

Comment should end in a period (godot)
type Reviewer struct {
Email string `json:"email"`
Name string `json:"name"`
}

type ExceptionGroup struct {
Action string `json:"action"`
CreatedAt string `json:"createdAt"`
ID string `json:"id"`
Justification string `json:"justification"`
ModifiedAt string `json:"modifiedAt"`
ReviewStatus string `json:"reviewStatus"`
ScopeMrn string `json:"scopeMrn"`
Title string `json:"title"`
Author Author `json:"author"`
Reviewer Reviewer `json:"reviewer"`
}

// ListExceptionGroupsResponse represents the GraphQL response structure

Check failure on line 1015 in internal/provider/gql.go

View workflow job for this annotation

GitHub Actions / Build

Comment should end in a period (godot)
type ListExceptionGroupsResponse struct {
ExceptionGroups []ExceptionGroup `json:"exceptionGroups"`
}

// ExtendedGqlClient represents a client to communicate with the GraphQL API

func (c *ExtendedGqlClient) ListExceptionGroups(
ctx context.Context,
scopeMrn string,
types []string,
) ([]ExceptionGroup, error) {
// Struct to hold the query response
var listExceptionGroups struct {
ExceptionGroups []ExceptionGroup `graphql:"exceptionGroups(input: $input)"`
}

// Helper function to convert string slices to *[]mondoov1.String
convertToGraphQLList := func(values []string) *[]mondoov1.ExceptionType {
if len(values) == 0 {
return nil
}
entries := []mondoov1.ExceptionType{}
for _, value := range values {
entries = append(entries, mondoov1.ExceptionType(value))
}
return &entries
}

// Prepare input for the query
input := mondoov1.ExceptionGroupsInput{
ScopeMrn: mondoov1.String(scopeMrn),
Types: convertToGraphQLList(types),
}
variables := map[string]interface{}{
"scopeMrn": mondoov1.String(scopeMrn),
"input": input,
}

err := c.Query(ctx, &q, variables)
if err != nil {
return nil, err
// Execute the query
if err := c.Query(ctx, &listExceptionGroups, variables); err != nil {
return nil, fmt.Errorf("failed to list exception groups: %w", err)
}

return q.ExceptionGroups, nil
return listExceptionGroups.ExceptionGroups, nil
}

// ListExceptionGroup
// func (c *ExtendedGqlClient) ListExceptionGroup(ctx context.Context, scopeMrn string) ([]mondoov1.ExceptionGroupsInput, error) {
// var q struct {
// ExceptionGroups []mondoov1.ExceptionGroupsInput `graphql:"exceptionGroups(scopeMrn: $scopeMrn)"`
// }
// variables := map[string]interface{}{
// "scopeMrn": mondoov1.String(scopeMrn),
// }

// err := c.Query(ctx, &q, variables)
// if err != nil {
// return nil, err
// }

// return q.ExceptionGroups, nil
// }

0 comments on commit 9c136f2

Please sign in to comment.