Skip to content

Commit

Permalink
Merge pull request #1411 from flanksource/oauth2-params
Browse files Browse the repository at this point in the history
Oauth2 params
  • Loading branch information
moshloop authored Nov 2, 2023
2 parents 1e54cc8 + 4f5a914 commit 1c26d33
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 21 deletions.
5 changes: 3 additions & 2 deletions api/v1/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ func (c Check) GetLabels() map[string]string {
}

type Oauth2Config struct {
Scopes []string `json:"scope,omitempty" yaml:"scope,omitempty"`
TokenURL string `json:"tokenURL,omitempty" yaml:"tokenURL,omitempty"`
Scopes []string `json:"scope,omitempty" yaml:"scope,omitempty"`
TokenURL string `json:"tokenURL,omitempty" yaml:"tokenURL,omitempty"`
Params map[string]string `json:"params,omitempty" yaml:"params,omitempty"`
}

type HTTPCheck struct {
Expand Down
7 changes: 7 additions & 0 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 17 additions & 8 deletions checks/folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,34 +89,43 @@ func checkLocalFolder(ctx *context.Context, check v1.FolderCheck) pkg.Results {
var results pkg.Results
results = append(results, result)
folders, err := getLocalFolderCheck(check.Path, check.Filter)
result.AddDetails(folders)

if err != nil {
return results.ErrorMessage(err)
}
result.AddDetails(folders)

if test := folders.Test(check.FolderTest); test != "" {
return results.Failf(test)
}
return results
}

func getLocalFolderCheck(path string, filter v1.FolderFilter) (*FolderCheck, error) {
func getLocalFolderCheck(path string, filter v1.FolderFilter) (FolderCheck, error) {
result := FolderCheck{}
_filter, err := filter.New()
if err != nil {
return nil, err
return result, err
}
if dir, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return result, nil
}
return result, err
} else if !dir.IsDir() {
return result, fmt.Errorf("%s is not a directory", path)
}
files, err := os.ReadDir(path)
if err != nil {
return nil, err
return result, err
}
if len(files) == 0 {
// directory is empty. returning duration of directory
info, err := os.Stat(path)
if err != nil {
return nil, err
return result, err
}
return &FolderCheck{
return FolderCheck{
Oldest: newFile(info),
Newest: newFile(info),
AvailableSize: SizeNotSupported,
Expand All @@ -126,15 +135,15 @@ func getLocalFolderCheck(path string, filter v1.FolderFilter) (*FolderCheck, err
for _, file := range files {
info, err := file.Info()
if err != nil {
return nil, err
return result, err
}
if file.IsDir() || !_filter.Filter(info) {
continue
}

result.Append(info)
}
return &result, err
return result, err
}

func getGenericFolderCheck(fs Filesystem, dir string, filter v1.FolderFilter) (*FolderCheck, error) {
Expand Down
2 changes: 1 addition & 1 deletion checks/folder_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type FolderCheck struct {
MaxSize *File `json:"largest,omitempty"`
TotalSize int64 `json:"size,omitempty"`
AvailableSize int64 `json:"availableSize,omitempty"`
Files []File `json:"files,omitempty"`
Files []File `json:"files"`
}

type File struct {
Expand Down
1 change: 1 addition & 0 deletions checks/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func (c *HTTPChecker) generateHTTPRequest(ctx *context.Context, check v1.HTTPChe
ClientSecret: connection.Password,
TokenURL: check.Oauth2.TokenURL,
Scopes: check.Oauth2.Scopes,
Params: check.Oauth2.Params,
})
}

Expand Down
20 changes: 11 additions & 9 deletions checks/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type SQLChecker interface {
}

type SQLDetails struct {
Rows []map[string]interface{} `json:"rows,omitempty"`
Rows []map[string]interface{} `json:"rows"`
Count int `json:"count,omitempty"`
}

Expand All @@ -28,23 +28,24 @@ type SQLDetails struct {
// Connects to a db using the specified `driver` and `connectionstring`
// Performs the test query given in `query`.
// Gives the single row test query result as result.
func querySQL(driver string, connection string, query string) (*SQLDetails, error) {
func querySQL(driver string, connection string, query string) (SQLDetails, error) {
result := SQLDetails{}
db, err := sql.Open(driver, connection)
if err != nil {
return nil, fmt.Errorf("failed to connect to db: %w", err)
return result, fmt.Errorf("failed to connect to db: %w", err)
}
defer db.Close()

rows, err := db.Query(query)
result := SQLDetails{}

if err != nil || rows.Err() != nil {
return nil, fmt.Errorf("failed to query db: %w", err)
return result, fmt.Errorf("failed to query db: %w", err)
}
defer rows.Close()

columns, err := rows.Columns()
if err != nil {
return nil, fmt.Errorf("failed to get columns: %w", err)
return result, fmt.Errorf("failed to get columns: %w", err)
}

for rows.Next() {
Expand All @@ -54,7 +55,7 @@ func querySQL(driver string, connection string, query string) (*SQLDetails, erro
rowValues[i] = &s
}
if err := rows.Scan(rowValues...); err != nil {
return nil, err
return result, err
}

var row = make(map[string]interface{})
Expand All @@ -71,7 +72,7 @@ func querySQL(driver string, connection string, query string) (*SQLDetails, erro
}

result.Count = len(result.Rows)
return &result, nil
return result, nil
}

// CheckSQL : Attempts to connect to a DB using the specified
Expand Down Expand Up @@ -110,11 +111,12 @@ func CheckSQL(ctx *context.Context, checker SQLChecker) pkg.Results { // nolint:
}

details, err := querySQL(checker.GetDriver(), connection.URL, query)
result.AddDetails(details)

if err != nil {
return results.ErrorMessage(err)
}

result.AddDetails(details)
if details.Count < check.Result {
return results.Failf("Query returned %d rows, expected %d", details.Count, check.Result)
}
Expand Down
4 changes: 4 additions & 0 deletions config/deploy/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3292,6 +3292,10 @@ spec:
oauth2:
description: Oauth2 Configuration. The client ID & Client secret should go to username & password respectively.
properties:
params:
additionalProperties:
type: string
type: object
scope:
items:
type: string
Expand Down
4 changes: 4 additions & 0 deletions config/deploy/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3292,6 +3292,10 @@ spec:
oauth2:
description: Oauth2 Configuration. The client ID & Client secret should go to username & password respectively.
properties:
params:
additionalProperties:
type: string
type: object
scope:
items:
type: string
Expand Down
8 changes: 8 additions & 0 deletions config/schemas/canary.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2416,6 +2416,14 @@
},
"tokenURL": {
"type": "string"
},
"params": {
"patternProperties": {
".*": {
"type": "string"
}
},
"type": "object"
}
},
"additionalProperties": false,
Expand Down
8 changes: 8 additions & 0 deletions config/schemas/component.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2678,6 +2678,14 @@
},
"tokenURL": {
"type": "string"
},
"params": {
"patternProperties": {
".*": {
"type": "string"
}
},
"type": "object"
}
},
"additionalProperties": false,
Expand Down
8 changes: 8 additions & 0 deletions config/schemas/health_http.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,14 @@
},
"tokenURL": {
"type": "string"
},
"params": {
"patternProperties": {
".*": {
"type": "string"
}
},
"type": "object"
}
},
"additionalProperties": false,
Expand Down
8 changes: 8 additions & 0 deletions config/schemas/topology.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2648,6 +2648,14 @@
},
"tokenURL": {
"type": "string"
},
"params": {
"patternProperties": {
".*": {
"type": "string"
}
},
"type": "object"
}
},
"additionalProperties": false,
Expand Down
14 changes: 13 additions & 1 deletion fixtures/datasources/folder_pass.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kind: Canary
metadata:
name: folder-pass
spec:
interval: 10
interval: 300
folder:
- path: /etc/
name: Check for updated /etc files
Expand Down Expand Up @@ -32,3 +32,15 @@ spec:
- name: new_files
value: results.?files.orValue([]).size()
type: counter
---
apiVersion: canaries.flanksource.com/v1
kind: Canary
metadata:
name: folder-pass-empty
spec:
interval: 300
folder:
- name: folder-nil-handling
path: /some/folder/that/does/not/exist
test:
expr: results.files.size() == 0

0 comments on commit 1c26d33

Please sign in to comment.