Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Create/Delete/Update methods to PolicyService #13

Merged
merged 7 commits into from
Aug 29, 2023

Conversation

takumakume
Copy link

Hi!

The following implementation was added to PolicyService:

  • Add necessary parameters
  • Add Create/Delete/Update
  • Add AddProject/DeleteProject
  • Add AddTag/DeleteTag
  • Add const variables PolicyOperator/PolicyViolationState

Confirmation that the following works with local DependencyTrack-v4.8.2:

import (
	"context"

	dtrack "github.com/DependencyTrack/client-go"
)

func main() {
	ctx := context.Background()
	client, err := dtrack.NewClient("http://localhost:8081", dtrack.WithAPIKey("..."))
	if err != nil {
		panic(err)
	}

	policy, err := client.Policy.Create(ctx, dtrack.Policy{
		Name:           "test",
		Operator:       dtrack.PolicyOperatorAny,
		ViolationState: dtrack.PolicyViolationStateFail,
	})
	if err != nil {
		panic(err)
	}
	fmt.Printf("created policy: %+v\n", policy)

	policy.Operator = dtrack.PolicyOperatorAll

	policyUpdated, err := client.Policy.Update(ctx, policy)
	if err != nil {
		panic(err)
	}

	fmt.Printf("updated policy: %+v\n", policyUpdated.Operator)

	existProjectUUID := uuid.MustParse("451b427e-cd46-45f0-98eb-63705c4dc624")
	policyAddedProject, err := client.Policy.AddProject(ctx, policyUpdated.UUID, existProjectUUID)
	if err != nil {
		panic(err)
	}

	fmt.Printf("added project to policy %s: %+v\n", policyAddedProject.UUID, policyAddedProject.Projects)

	policyAddedTag, err := client.Policy.AddTag(ctx, policyAddedProject.UUID, "test")
	if err != nil {
		panic(err)
	}

	fmt.Printf("added tag to policy %s: %+v\n", policyAddedTag.UUID, policyAddedTag.Tags)

	if err := client.Policy.Delete(ctx, policyAddedTag.UUID); err != nil {
		panic(err)
	}
}
created policy: {UUID:1e4e1c68-70d0-46fe-a2a3-178aba9ad9ff Name:test Operator:ANY ViolationState:INFO PolicyConditions:[] IncludeChildren:false Global:true Projects:[] Tags:[]}
updated policy: ALL
added project to policy 1e4e1c68-70d0-46fe-a2a3-178aba9ad9ff: [{UUID:451b427e-cd46-45f0-98eb-63705c4dc624 Author: Publisher: Group: Name:test Description: Version:latest Classifier:CONTAINER CPE: PURL: SWIDTagID: DirectDependencies:[{"name":"metrics-sidecar","uuid":"01442148-a3f8-4352-9bce-d78f3f55c711","objectType":"COMPONENT"}] Properties:[] Tags:[{Name:test}] Active:true Metrics:{FirstOccurrence:0 LastOccurrence:0 InheritedRiskScore:0 Vulnerabilities:0 VulnerableComponents:0 Components:0 Suppressed:0 Critical:0 High:0 Medium:0 Low:0 Unassigned:0 FindingsTotal:0 FindingsAudited:0 FindingsUnaudited:0 PolicyViolationsTotal:0 PolicyViolationsFail:0 PolicyViolationsWarn:0 PolicyViolationsInfo:0 PolicyViolationsAudited:0 PolicyViolationsUnaudited:0 PolicyViolationsSecurityTotal:0 PolicyViolationsSecurityAudited:0 PolicyViolationsSecurityUnaudited:0 PolicyViolationsLicenseTotal:0 PolicyViolationsLicenseAudited:0 PolicyViolationsLicenseUnaudited:0 PolicyViolationsOperationalTotal:0 PolicyViolationsOperationalAudited:0 PolicyViolationsOperationalUnaudited:0} ParentRef:<nil> LastBOMImport:1692796438570}]
added tag to policy 1e4e1c68-70d0-46fe-a2a3-178aba9ad9ff: [{Name:test}]

Signed-off-by: 久米 拓馬 <takuma.kume@pepabo.com>
Signed-off-by: 久米 拓馬 <takuma.kume@pepabo.com>
Signed-off-by: 久米 拓馬 <takuma.kume@pepabo.com>
Signed-off-by: 久米 拓馬 <takuma.kume@pepabo.com>
Signed-off-by: 久米 拓馬 <takuma.kume@pepabo.com>
Signed-off-by: 久米 拓馬 <takuma.kume@pepabo.com>
Signed-off-by: 久米 拓馬 <takuma.kume@pepabo.com>
Copy link
Member

@nscuro nscuro left a comment

Choose a reason for hiding this comment

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

Thanks yet again @takumakume! 🚀

@nscuro nscuro merged commit 9894000 into DependencyTrack:main Aug 29, 2023
4 checks passed
@takumakume takumakume deleted the add-policy-methods branch September 8, 2023 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants