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

Narrow 🦉 getter interface #570

Merged
merged 2 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions internal/owl/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,48 @@ func resolveDotEnv() graphql.FieldResolveFn {
}
}

func resolveGetter() graphql.FieldResolveFn {
return func(p graphql.ResolveParams) (interface{}, error) {
key := p.Args["key"].(string)
insecure := p.Args["insecure"].(bool)
kv := &SetVarItem{}
var opSet *OperationSet

switch p.Source.(type) {
case nil, string:
// root passes string
return kv, nil
case *OperationSet:
opSet = p.Source.(*OperationSet)
default:
return nil, errors.New("source is not an OperationSet")
}

val, ok := opSet.values[key]
if !ok {
return kv, nil
}

kv.Var = val.Var
kv.Value = val.Value

spec, ok := opSet.specs[key]
if ok {
kv.Spec = spec.Spec
}

// up-graph?
if !insecure {
original := kv.Value.Original
kv.Value.Status = "MASKED"
kv.Value.Original = ""
kv.Value.Resolved = strings.Repeat("*", max(8, len(original)))
}

return kv, nil
}
}

func resolveSnapshot() graphql.FieldResolveFn {
return func(p graphql.ResolveParams) (interface{}, error) {
insecure := p.Args["insecure"].(bool)
Expand Down Expand Up @@ -638,6 +680,20 @@ func init() {
},
Resolve: resolveDotEnv(),
},
"get": &graphql.Field{
Type: graphql.NewNonNull(VariableType),
Args: graphql.FieldConfigArgument{
"key": &graphql.ArgumentConfig{
Type: graphql.String,
DefaultValue: "",
},
"insecure": &graphql.ArgumentConfig{
Type: graphql.Boolean,
DefaultValue: false,
},
},
Resolve: resolveGetter(),
},
"sensitiveKeys": &graphql.Field{
Type: graphql.NewNonNull(graphql.NewList(VariableType)),
Resolve: resolveSensitive(),
Expand Down
21 changes: 21 additions & 0 deletions internal/owl/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,27 @@ func Test_Graph_Sensitive(t *testing.T) {
testCases.runAll(t)
}

func Test_Graph_Get(t *testing.T) {
testCases := fileTestCases{
{
name: "InsecureGet",
post: func(t *testing.T, result *graphql.Result) {
render, err := extractDataKey(result.Data, "render")
require.NoError(t, err)
require.NotNil(t, render)

b, err := yaml.Marshal(render)
// b, err := json.MarshalIndent(result, "", " ")
require.NoError(t, err)
_, _ = fmt.Println(string(b))
require.NotNil(t, b)
},
},
}

testCases.runAll(t)
}

func Test_Graph_DotEnv(t *testing.T) {
testCases := fileTestCases{
{
Expand Down
237 changes: 237 additions & 0 deletions internal/owl/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,82 @@ func (s *Store) sensitiveQuery(query, vars io.StringWriter) error {
return nil
}

func (s *Store) getterQuery(query, vars io.StringWriter) error {
varDefs := []*ast.VariableDefinition{
ast.NewVariableDefinition(&ast.VariableDefinition{
Variable: ast.NewVariable(&ast.Variable{
Name: ast.NewName(&ast.Name{
Value: "key",
}),
}),
Type: ast.NewNamed(&ast.Named{
Name: ast.NewName(&ast.Name{
Value: "String",
}),
}),
DefaultValue: ast.NewStringValue(&ast.StringValue{
Value: "",
}),
}),
ast.NewVariableDefinition(&ast.VariableDefinition{
Variable: ast.NewVariable(&ast.Variable{
Name: ast.NewName(&ast.Name{
Value: "insecure",
}),
}),
Type: ast.NewNamed(&ast.Named{
Name: ast.NewName(&ast.Name{
Value: "Boolean",
}),
}),
DefaultValue: ast.NewBooleanValue(&ast.BooleanValue{
Value: false,
}),
}),
}

loaded, updated, deleted := 0, 0, 0
for _, opSet := range s.opSets {
if len(opSet.specs) == 0 && len(opSet.values) == 0 {
continue
}
switch opSet.operation.kind {
case LoadSetOperation:
loaded++
case UpdateSetOperation:
updated++
case DeleteSetOperation:
deleted++
}

}
s.logger.Debug("getter opSets breakdown", zap.Int("loaded", loaded), zap.Int("updated", updated), zap.Int("deleted", deleted), zap.Int("total", len(s.opSets)))

q, err := NewQuery("Get", varDefs,
[]QueryNodeReducer{
reconcileAsymmetry(s),
reduceSetOperations(s, vars),
reduceSepcs(s),
reduceGetter(),
},
)
if err != nil {
return err
}

text, err := q.Print()
if err != nil {
return err
}

_, err = query.WriteString(text)
if err != nil {
return err
}

return nil
}

func reduceSetOperations(store *Store, vars io.StringWriter) QueryNodeReducer {
return func(opDef *ast.OperationDefinition, selSet *ast.SelectionSet) (*ast.SelectionSet, error) {
opSetData := make(map[string]SetVarItems, len(store.opSets))
Expand Down Expand Up @@ -331,6 +407,167 @@ func reduceSensitive() QueryNodeReducer {
}
}

func reduceGetter() QueryNodeReducer {
return func(opDef *ast.OperationDefinition, selSet *ast.SelectionSet) (*ast.SelectionSet, error) {
nextSelSet := ast.NewSelectionSet(&ast.SelectionSet{
Selections: []ast.Selection{
ast.NewField(&ast.Field{
Name: ast.NewName(&ast.Name{
Value: "var",
}),
SelectionSet: ast.NewSelectionSet(&ast.SelectionSet{
Selections: []ast.Selection{
ast.NewField(&ast.Field{
Name: ast.NewName(&ast.Name{
Value: "key",
}),
}),
ast.NewField(&ast.Field{
Name: ast.NewName(&ast.Name{
Value: "origin",
}),
}),
ast.NewField(&ast.Field{
Name: ast.NewName(&ast.Name{
Value: "created",
}),
}),
ast.NewField(&ast.Field{
Name: ast.NewName(&ast.Name{
Value: "updated",
}),
}),
ast.NewField(&ast.Field{
Name: ast.NewName(&ast.Name{
Value: "operation",
}),
SelectionSet: ast.NewSelectionSet(&ast.SelectionSet{
Selections: []ast.Selection{
ast.NewField(&ast.Field{
Name: ast.NewName(&ast.Name{
Value: "source",
}),
}),
},
}),
}),
},
}),
}),
ast.NewField(&ast.Field{
Name: ast.NewName(&ast.Name{
Value: "value",
}),
SelectionSet: ast.NewSelectionSet(&ast.SelectionSet{
Selections: []ast.Selection{
// ast.NewField(&ast.Field{
// Name: ast.NewName(&ast.Name{
// Value: "type",
// }),
// }),
ast.NewField(&ast.Field{
Name: ast.NewName(&ast.Name{
Value: "original",
}),
}),
ast.NewField(&ast.Field{
Name: ast.NewName(&ast.Name{
Value: "resolved",
}),
}),
ast.NewField(&ast.Field{
Name: ast.NewName(&ast.Name{
Value: "status",
}),
}),
},
}),
}),
ast.NewField(&ast.Field{
Name: ast.NewName(&ast.Name{
Value: "spec",
}),
SelectionSet: ast.NewSelectionSet(&ast.SelectionSet{
Selections: []ast.Selection{
ast.NewField(&ast.Field{
Name: ast.NewName(&ast.Name{
Value: "name",
}),
}),
ast.NewField(&ast.Field{
Name: ast.NewName(&ast.Name{
Value: "required",
}),
}),
},
}),
}),
// ast.NewField(&ast.Field{
// Name: ast.NewName(&ast.Name{
// Value: "errors",
// }),
// SelectionSet: ast.NewSelectionSet(&ast.SelectionSet{
// Selections: []ast.Selection{
// ast.NewField(&ast.Field{
// Name: ast.NewName(&ast.Name{
// Value: "code",
// }),
// }),
// ast.NewField(&ast.Field{
// Name: ast.NewName(&ast.Name{
// Value: "message",
// }),
// }),
// },
// }),
// }),
},
})

selSet.Selections = append(selSet.Selections,
ast.NewField(&ast.Field{
Name: ast.NewName(&ast.Name{
Value: "render",
}),
SelectionSet: ast.NewSelectionSet(&ast.SelectionSet{
Selections: []ast.Selection{
ast.NewField(&ast.Field{
Name: ast.NewName(&ast.Name{
Value: "get",
}),
Arguments: []*ast.Argument{
ast.NewArgument(&ast.Argument{
Name: ast.NewName(&ast.Name{
Value: "key",
}),
Value: ast.NewVariable(&ast.Variable{
Name: ast.NewName(&ast.Name{
Value: "key",
}),
}),
}),
ast.NewArgument(&ast.Argument{
Name: ast.NewName(&ast.Name{
Value: "insecure",
}),
Value: ast.NewVariable(&ast.Variable{
Name: ast.NewName(&ast.Name{
Value: "insecure",
}),
}),
}),
},
SelectionSet: nextSelSet,
}),
},
}),
}),
)

return nextSelSet, nil
}
}

func reduceSnapshot() QueryNodeReducer {
return func(opDef *ast.OperationDefinition, selSet *ast.SelectionSet) (*ast.SelectionSet, error) {
nextSelSet := ast.NewSelectionSet(&ast.SelectionSet{
Expand Down
Loading
Loading