nanjiek commented on code in PR #732: URL: https://github.com/apache/dubbo-go-pixiu/pull/732#discussion_r2272467535
########## pkg/filter/opa/opa.go: ########## @@ -0,0 +1,142 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package opa + +import ( + "context" + "fmt" +) + +import ( + "github.com/open-policy-agent/opa/rego" +) + +import ( + "github.com/apache/dubbo-go-pixiu/pkg/common/constant" + "github.com/apache/dubbo-go-pixiu/pkg/common/extension/filter" + "github.com/apache/dubbo-go-pixiu/pkg/context/http" + "github.com/apache/dubbo-go-pixiu/pkg/logger" +) + +const ( + Kind = constant.HTTPAuthOPAFilter +) + +func init() { + filter.RegisterHttpFilter(&Plugin{}) +} + +type ( + Plugin struct{} + + FilterFactory struct { + cfg *Config + preparedQuery *rego.PreparedEvalQuery + } + + Filter struct { + cfg *Config + preparedQuery *rego.PreparedEvalQuery + } + + Config struct { + Policy string `yaml:"policy" json:"policy" mapstructure:"policy"` + Entrypoint string `yaml:"entrypoint" json:"entrypoint" mapstructure:"entrypoint"` + } +) + +func (p *Plugin) Kind() string { + return Kind +} + +func (p *Plugin) CreateFilterFactory() (filter.HttpFilterFactory, error) { + return &FilterFactory{cfg: &Config{}}, nil +} + +func (factory *FilterFactory) Config() any { + return factory.cfg +} + +// Apply is called after the configuration is loaded and is used to prepare the OPA query. +func (factory *FilterFactory) Apply() error { + policy := factory.cfg.Policy + if policy == "" { + return fmt.Errorf("OPA policy is empty in the configuration") + } + + // Create a new Rego instance, loading the policy directly from the string. + // We also specify the entrypoint for the policy evaluation. + r := rego.New( + rego.Query(factory.cfg.Entrypoint), + rego.Module("policy.rego", policy), + ) + + // Prepare the query for evaluation. This step compiles the policy. + preparedQuery, err := r.PrepareForEval(context.Background()) Review Comment: This is a good idea. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org For additional commands, e-mail: notifications-h...@dubbo.apache.org