-
Notifications
You must be signed in to change notification settings - Fork 0
Add retraced commands [ch3179] #45
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
Merged
dexhorthy
merged 1 commit into
replicatedcom:master
from
dexhorthy:dex/ch3179/replicated-audit-logs
Nov 9, 2017
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package planners | ||
|
|
||
| import ( | ||
| "github.com/pkg/errors" | ||
| "github.com/replicatedcom/support-bundle/pkg/plans" | ||
| "github.com/replicatedcom/support-bundle/pkg/plugins/retraced/producers" | ||
| "github.com/replicatedcom/support-bundle/pkg/types" | ||
| ) | ||
|
|
||
| func Events(spec types.Spec) []types.Task { | ||
| var err error | ||
| if spec.RetracedEventsCommand == nil { | ||
| err = errors.New("spec for retraced.events required") | ||
| } else if spec.RetracedEventsCommand.APIEndpoint == "" { // require an endpoint, I don't think defaulting to SaaS Retraced makes sense | ||
| err = errors.New("retraced.events spec missing api_endpoint") | ||
| } else if spec.RetracedEventsCommand.APIToken == "" { | ||
| err = errors.New("retraced.events spec missing api_token") | ||
| } else if spec.RetracedEventsCommand.ProjectID == "" { | ||
| err = errors.New("retraced.events spec missing project_id") | ||
| } | ||
| if err != nil { | ||
| task := plans.PreparedError(err, spec) | ||
| return []types.Task{task} | ||
| } | ||
| return []types.Task{ | ||
| &plans.StreamSource{ | ||
| Producer: producers.Events(spec), | ||
| RawPath: spec.Raw, | ||
| }, | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package retraced | ||
|
|
||
| import ( | ||
| "github.com/replicatedcom/support-bundle/pkg/plugins/retraced/planners" | ||
| "github.com/replicatedcom/support-bundle/pkg/types" | ||
| ) | ||
|
|
||
| func New() types.Plugin { | ||
| return map[string]types.Planner{ | ||
| "events": planners.Events, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package producers | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "io" | ||
| "net/http" | ||
|
|
||
| "github.com/pkg/errors" | ||
| "github.com/replicatedcom/support-bundle/pkg/types" | ||
| "github.com/retracedhq/retraced-go" | ||
| "github.com/spf13/jwalterweatherman" | ||
| ) | ||
|
|
||
| var ( | ||
| defaultMask = &retraced.EventNodeMask{ | ||
| Action: true, | ||
| CRUD: true, | ||
| CanonicalTime: true, | ||
| } | ||
| defaultQuery = &retraced.StructuredQuery{ | ||
| CRUD: "c,u,d", | ||
| } | ||
| ) | ||
|
|
||
| func Events(spec types.Spec) types.StreamProducer { | ||
| return func(ctx context.Context) (io.Reader, error) { | ||
|
|
||
| client, err := getClient(spec) | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "create retraced client") | ||
| } | ||
|
|
||
| query := getQuery(spec) | ||
| mask := getMask(spec) | ||
| reader, writer := io.Pipe() | ||
|
|
||
| go func() { | ||
| var err error | ||
| defer func() { | ||
| writer.CloseWithError(err) | ||
| }() | ||
| err = client.ExportCSV(ctx, writer, query, mask) | ||
| if err != nil { | ||
| jwalterweatherman.ERROR.Printf("Failed to collect retraced events %v", err) | ||
| } | ||
| }() | ||
|
|
||
| return reader, nil | ||
| } | ||
| } | ||
| func getClient(spec types.Spec) (*retraced.Client, error) { | ||
| client, err := retraced.NewClient(spec.RetracedEventsCommand.ProjectID, spec.RetracedEventsCommand.APIToken) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if spec.RetracedEventsCommand.Insecure { | ||
| client.HttpClient = &http.Client{ | ||
| Transport: &http.Transport{ | ||
| TLSClientConfig: &tls.Config{ | ||
| InsecureSkipVerify: true, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
| return client, nil | ||
| } | ||
|
|
||
| func getQuery(spec types.Spec) *retraced.StructuredQuery { | ||
| var query *retraced.StructuredQuery | ||
| if spec.RetracedEventsCommand.Query != nil { | ||
| query = spec.RetracedEventsCommand.Query | ||
| } else { | ||
| query = defaultQuery | ||
| } | ||
| return query | ||
| } | ||
|
|
||
| func getMask(spec types.Spec) *retraced.EventNodeMask { | ||
| var mask *retraced.EventNodeMask | ||
| if spec.RetracedEventsCommand.Mask != nil { | ||
| mask = spec.RetracedEventsCommand.Mask | ||
| } else { | ||
| mask = defaultMask | ||
| } | ||
| return mask | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package ginkgo | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "strings" | ||
|
|
||
| . "github.com/onsi/ginkgo" | ||
| . "github.com/onsi/gomega" | ||
| "github.com/replicatedcom/support-bundle/pkg/types" | ||
| ) | ||
|
|
||
| var _ = Describe("The retraced.events spec", func() { | ||
| opts := types.RetracedEventsOptions{ | ||
| APIEndpoint: os.Getenv("RETRACED_API_ENDPOINT"), | ||
| APIToken: os.Getenv("RETRACED_API_TOKEN"), | ||
| ProjectID: os.Getenv("RETRACED_PROJECT_ID"), | ||
| Insecure: os.Getenv("RETRACED_INSECURE_SKIP_VERIFY") != "", | ||
| } | ||
|
|
||
| BeforeEach(EnterNewTempDir) | ||
| BeforeEach(func() { | ||
| Expect(opts.APIEndpoint).NotTo(BeEmpty(), "RETRACED_API_ENDPOINT must be set") | ||
| Expect(opts.APIToken).NotTo(BeEmpty(), "RETRACED_API_TOKEN must be set") | ||
| Expect(opts.ProjectID).NotTo(BeEmpty(), "RETRACED_PROJECT_ID must be set") | ||
| }) | ||
| AfterEach(CleanupDir) | ||
|
|
||
| It("Collects events from retraced", func() { | ||
| WriteBundleConfig(` | ||
| specs: | ||
| - builtin: retraced.events | ||
| raw: /audit/events.txt | ||
| retraced.events: | ||
| api_endpoint: ` + opts.APIEndpoint + ` | ||
| api_token: ` + opts.APIToken + ` | ||
| project_id: ` + opts.ProjectID + ` | ||
| insecure: ` + fmt.Sprintf("%v", opts.Insecure)) | ||
|
|
||
| GenerateBundle() | ||
|
|
||
| Expect(err).NotTo(HaveOccurred()) | ||
| errors := GetFileFromBundle("error.json") | ||
| Expect(errors).To(Equal("null")) | ||
|
|
||
| contents := GetFileFromBundle("audit/events.txt") | ||
| Expect(contents).ToNot(BeEmpty()) | ||
|
|
||
| header := strings.Split(contents, "\n")[0] | ||
| Expect(header).To(Equal("action,crud,canonical_time")) | ||
|
|
||
| }) | ||
|
|
||
| It("Allows for a custom mask+query", func() { | ||
| WriteBundleConfig(` | ||
| specs: | ||
| - builtin: retraced.events | ||
| raw: /audit/events.txt | ||
| retraced.events: | ||
| api_endpoint: ` + opts.APIEndpoint + ` | ||
| api_token: ` + opts.APIToken + ` | ||
| project_id: ` + opts.ProjectID + ` | ||
| insecure: ` + fmt.Sprintf("%v", opts.Insecure) + ` | ||
| mask: | ||
| CRUD: true | ||
| query: | ||
| CRUD: r`) | ||
|
|
||
| GenerateBundle() | ||
|
|
||
| Expect(err).NotTo(HaveOccurred()) | ||
| errors := GetFileFromBundle("error.json") | ||
| Expect(errors).To(Equal("null")) | ||
|
|
||
| contents := GetFileFromBundle("audit/events.txt") | ||
| Expect(contents).ToNot(BeEmpty()) | ||
|
|
||
| lines := strings.Split(contents, "\n") | ||
| Expect(lines[0]).To(Equal("crud")) | ||
| for _, line := range lines[1:] { | ||
| if line != "" { | ||
| Expect(line).To(Equal("r")) | ||
| } | ||
| } | ||
|
|
||
| }) | ||
| }) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about doing a nil check here on spec.RetracedEventsCommand and then passing as value to producers.Events?
https://github.com/replicatedcom/support-bundle/blob/master/pkg/plugins/docker/planners/run-command.go#L11
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it 👍