From: gururajarao79 Date: Thu, 27 Feb 2025 10:43:27 +0000 (+0100) Subject: remove references of bundleserver from phase1 X-Git-Tag: 1.0.3~8 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=faedb7d439f0a864ad976e8b8c62a03d39bb4b3f;p=policy%2Fopa-pdp.git remove references of bundleserver from phase1 Issue-ID: POLICY-5295 Change-Id: I33f498cf6b1253dfa273be38ac7d6e8438d931ab Signed-off-by: gururajarao79 --- diff --git a/Dockerfile b/Dockerfile index e00ce84..5d2c43e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -52,7 +52,7 @@ COPY --from=compile /app /app # Copy our opa executable from build stage COPY --from=build /tmp/opa /app/opa -RUN chmod +x /app/opa-pdp && chmod 755 /app/opa +RUN chmod +x /app/opa-pdp && chmod 755 /app/opa && chmod 777 /app/bundles # Switch to the non-root user and 1000 is for ubuntu diff --git a/api/register-handlers.go b/api/register-handlers.go index 34878c3..49b953f 100644 --- a/api/register-handlers.go +++ b/api/register-handlers.go @@ -24,7 +24,6 @@ package api import ( "net/http" "policy-opa-pdp/cfg" - "policy-opa-pdp/pkg/bundleserver" "policy-opa-pdp/pkg/data" "policy-opa-pdp/pkg/decision" "policy-opa-pdp/pkg/healthcheck" @@ -40,10 +39,6 @@ func RegisterHandlers() { opaDecisionHandler := http.HandlerFunc(decision.OpaDecision) http.Handle("/policy/pdpo/v1/decision", basicAuth(opaDecisionHandler)) - //This api is used internally by OPA-SDK - bundleServerHandler := http.HandlerFunc(bundleserver.GetBundle) - http.Handle("/opa/bundles/", bundleServerHandler) - // Handler for kubernetes readiness probe readinessProbeHandler := http.HandlerFunc(readinessProbe) http.Handle("/ready", readinessProbeHandler) diff --git a/api/register-handlers_test.go b/api/register-handlers_test.go index f67c1f5..3cbd248 100644 --- a/api/register-handlers_test.go +++ b/api/register-handlers_test.go @@ -1,6 +1,6 @@ // - // ========================LICENSE_START================================= -// Copyright (C) 2024: Deutsche Telekom +// Copyright (C) 2024-2025: Deutsche Telekom // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -23,7 +23,6 @@ import ( "net/http" "net/http/httptest" "policy-opa-pdp/cfg" - "policy-opa-pdp/pkg/bundleserver" "policy-opa-pdp/pkg/decision" "policy-opa-pdp/pkg/healthcheck" "testing" @@ -44,7 +43,6 @@ func TestRegisterHandlers(t *testing.T) { statusCode int }{ {"/policy/pdpo/v1/decision", decision.OpaDecision, http.StatusUnauthorized}, - {"/opa/bundles/", bundleserver.GetBundle, http.StatusInternalServerError}, {"/ready", readinessProbe, http.StatusOK}, {"/policy/pdpo/v1/healthcheck", healthcheck.HealthCheckHandler, http.StatusUnauthorized}, } diff --git a/cmd/opa-pdp/opa-pdp.go b/cmd/opa-pdp/opa-pdp.go index 468a193..3a8a52f 100644 --- a/cmd/opa-pdp/opa-pdp.go +++ b/cmd/opa-pdp/opa-pdp.go @@ -25,12 +25,10 @@ import ( "context" "net/http" "os" - "os/exec" "os/signal" h "policy-opa-pdp/api" "policy-opa-pdp/cfg" "policy-opa-pdp/consts" - "policy-opa-pdp/pkg/bundleserver" "policy-opa-pdp/pkg/kafkacomm" "policy-opa-pdp/pkg/kafkacomm/handler" "policy-opa-pdp/pkg/kafkacomm/publisher" @@ -48,7 +46,6 @@ var ( // Declare function variables for dependency injection makes it more testable var ( initializeHandlersFunc = initializeHandlers - initializeBundleFunc = initializeBundle startHTTPServerFunc = startHTTPServer shutdownHTTPServerFunc = shutdownHTTPServer waitForServerFunc = waitForServer @@ -68,10 +65,6 @@ func main() { // Initialize Handlers and Build Bundle initializeHandlersFunc() - if output, err := initializeBundleFunc(exec.Command); err != nil { - log.Warnf("Output %s", string(output)) - log.Warnf("Failed to initialize bundle: %s", err) - } // Start HTTP Server server := startHTTPServerFunc() @@ -144,11 +137,6 @@ func initializeHandlers() { h.RegisterHandlers() } -// build bundle tar file -func initializeBundle(execCmd func(string, ...string) *exec.Cmd) (string, error) { - return bundleserver.BuildBundle(execCmd) -} - func startHTTPServer() *http.Server { //Configures the HTTP server to wait a maximum of 5 seconds for the headers of incoming requests server := &http.Server{Addr: consts.ServerPort, ReadHeaderTimeout: 5 * time.Second} diff --git a/cmd/opa-pdp/opa-pdp_test.go b/cmd/opa-pdp/opa-pdp_test.go index 60bae08..bc3aba6 100644 --- a/cmd/opa-pdp/opa-pdp_test.go +++ b/cmd/opa-pdp/opa-pdp_test.go @@ -25,7 +25,6 @@ import ( "errors" "net/http" "os" - "os/exec" "policy-opa-pdp/consts" "policy-opa-pdp/pkg/kafkacomm" "policy-opa-pdp/pkg/kafkacomm/mocks" @@ -159,11 +158,6 @@ func SetupMocks() { log.Debug("Handlers initialized") } - // Mock initializeBundle - initializeBundleFunc = func(cmdFn func(string, ...string) *exec.Cmd) (string, error) { - return "", nil // no error expected - } - // Use an actual *http.Server instance for testing testServer := &http.Server{} @@ -304,16 +298,6 @@ func TestKafkaNilConsumerInitialization(t *testing.T){ assert.True(t, true, "main function executed successfully") } - -// Test to validate that the OPA bundle initialization process works as expected. -func TestInitializeBundle(t *testing.T) { - mockExecCmd := func(name string, arg ...string) *exec.Cmd { - return exec.Command("echo") - } - output,err := initializeBundle(mockExecCmd) - assert.NoError(t, err, output) -} - // Test to verify that the HTTP server starts successfully. func TestStartHTTPServer(t *testing.T) { server := startHTTPServer() @@ -514,26 +498,6 @@ func TestHandleMessages(t *testing.T) { } - -// Test to simulate a failure during OPA bundle initialization in the main function. -func TestMain_InitializeBundleFailure(t *testing.T) { - initializeBundleFunc = func(cmdFn func(string, ...string) *exec.Cmd) (string, error) { - return "Bundle Initialization Error", errors.New("bundle initialization error") // Simulate error - } - - done := make(chan struct{}) - go func() { - main() - close(done) - }() - - select { - case <-done: - case <-time.After(1 * time.Second): - t.Error("main function timed out on initializeBundleFunc failure") - } -} - // Test to simulate a Kafka initialization failure in the main function. func TestMain_KafkaInitializationFailure(t *testing.T) { startKafkaConsAndProdFunc = func() (*kafkacomm.KafkaConsumer, *kafkacomm.KafkaProducer, error) { diff --git a/pkg/bundleserver/bundle-server.go b/pkg/bundleserver/bundle-server.go deleted file mode 100644 index bd88f23..0000000 --- a/pkg/bundleserver/bundle-server.go +++ /dev/null @@ -1,66 +0,0 @@ -// - -// ========================LICENSE_START================================= -// Copyright (C) 2024-2025: Deutsche Telecom -// -// Licensed 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. -// SPDX-License-Identifier: Apache-2.0 -// ========================LICENSE_END=================================== - -// Package bundleserver provides functionalities for serving and building OPA bundles. -// This package includes functions to handle HTTP requests for bundles and -// to build OPA bundles using specified commands -package bundleserver - -import ( - "net/http" - "os" - "os/exec" - "policy-opa-pdp/consts" - "policy-opa-pdp/pkg/log" - "time" -) - -// handles HTTP requests to serve the OPA bundle -func GetBundle(res http.ResponseWriter, req *http.Request) { - log.Debugf("PDP received a Bundle request.") - - file, err := os.Open(consts.BundleTarGzFile) - - if err != nil { - log.Warnf("Bundle server could not serve the request ::: %s", err) - res.WriteHeader(http.StatusInternalServerError) - return - } - defer file.Close() - - res.Header().Set("Content-Type", "application/octet-stream") - res.Header().Set("Content-Disposition", "attachment; filename="+consts.BundleTarGz) - res.Header().Set("Content-Transfer-Encoding", "binary") - res.Header().Set("Expires", "0") - http.ServeContent(res, req, "Bundle Request Response", time.Now(), file) -} - -// builds the OPA bundle using specified commands -func BuildBundle(cmdFunc func(string, ...string) *exec.Cmd) (string, error) { - cmd := cmdFunc(consts.Opa, consts.BuildBundle, consts.V1_COMPATIBLE, consts.Policies, consts.Data, consts.Output, consts.BundleTarGzFile) - log.Debugf("Before calling combinedoutput") - output, err := cmd.CombinedOutput() - - if err != nil { - log.Warnf("Error output : %s", string(output)) - log.Warnf("Failed to build Bundle: %v", err) - return string(output), err - } - log.Debug("Bundle Built Sucessfully....") - return string(output), nil -} diff --git a/pkg/bundleserver/bundle-server_test.go b/pkg/bundleserver/bundle-server_test.go deleted file mode 100644 index 9c4f95d..0000000 --- a/pkg/bundleserver/bundle-server_test.go +++ /dev/null @@ -1,118 +0,0 @@ -// - -// ========================LICENSE_START================================= -// Copyright (C) 2024-2025: Deutsche Telekom -// -// Licensed 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. -// SPDX-License-Identifier: Apache-2.0 -// ========================LICENSE_END=================================== -// - -package bundleserver - -import ( - "net/http" - "net/http/httptest" - "os" - "os/exec" - "policy-opa-pdp/consts" - "testing" -) - -// Mock function for exec.Command -func mockCmd(command string, args ...string) *exec.Cmd { - cs := []string{"-test.run=TestHelperProcess", "--", command} - cs = append(cs, args...) - cmd := exec.Command(os.Args[0], cs...) - cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"} - return cmd -} - -// TestHelperProcess is a helper process used by mockCmd -func TestHelperProcess(*testing.T) { - if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { - return - } - os.Exit(0) -} - -func TestGetBundle(t *testing.T) { - // Create a temporary file to simulate the bundle file - tmpFile, err := os.CreateTemp("", "bundle-*.tar.gz") - if err != nil { - t.Fatalf("Failed to create temp file: %v", err) - } - defer os.Remove(tmpFile.Name()) - - consts.BundleTarGzFile = tmpFile.Name() - - req, err := http.NewRequest("GET", "/bundle", nil) - if err != nil { - t.Fatalf("Failed to create request: %v", err) - } - - rr := httptest.NewRecorder() - handler := http.HandlerFunc(GetBundle) - - handler.ServeHTTP(rr, req) - - if status := rr.Code; status != http.StatusOK { - t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) - } - - expected := "attachment; filename=" + consts.BundleTarGz - if rr.Header().Get("Content-Disposition") != expected { - t.Errorf("handler returned unexpected header: got %v want %v", rr.Header().Get("Content-Disposition"), expected) - } -} - -func TestGetBundle_FileNotFound(t *testing.T) { - consts.BundleTarGzFile = "nonexistent-file.tar.gz" - - req, err := http.NewRequest("GET", "/bundle", nil) - if err != nil { - t.Fatalf("Failed to create request: %v", err) - } - - rr := httptest.NewRecorder() - handler := http.HandlerFunc(GetBundle) - - handler.ServeHTTP(rr, req) - - if status := rr.Code; status != http.StatusInternalServerError { - t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusInternalServerError) - } -} - -func TestBuildBundle(t *testing.T) { - output, err := BuildBundle(mockCmd) - if err != nil { - t.Errorf("BuildBundle() error = %v, wantErr %v", err, output) - } -} - -func TestBuildBundle_CommandFailure(t *testing.T) { - // Mock function to simulate command failure - mockCmdFail := func(command string, args ...string) *exec.Cmd { - cs := []string{"-test.run=TestHelperProcess", "--", command} - cs = append(cs, args...) - cmd := exec.Command(os.Args[0], cs...) - cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"} - cmd.Stderr = os.Stderr - return cmd - } - - output, err := BuildBundle(mockCmdFail) - if err == nil { - t.Errorf("BuildBundle() error = nil, wantErr %v", output) - } -} diff --git a/pkg/kafkacomm/handler/pdp_update_message_handler.go b/pkg/kafkacomm/handler/pdp_update_message_handler.go index 5842c29..58ee1b0 100644 --- a/pkg/kafkacomm/handler/pdp_update_message_handler.go +++ b/pkg/kafkacomm/handler/pdp_update_message_handler.go @@ -24,7 +24,6 @@ import ( "fmt" "os/exec" "policy-opa-pdp/consts" - "policy-opa-pdp/pkg/bundleserver" "policy-opa-pdp/pkg/kafkacomm/publisher" "policy-opa-pdp/pkg/log" "policy-opa-pdp/pkg/model" @@ -138,7 +137,7 @@ func pdpUpdateMessageHandler(message []byte, p publisher.PdpStatusSender) error // build bundle tar file func createBundleFunc(execCmd func(string, ...string) *exec.Cmd, toscaPolicy model.ToscaPolicy) (string, error) { - return bundleserver.BuildBundle(execCmd) + return utils.BuildBundle(execCmd) } func sendSuccessResponse(p publisher.PdpStatusSender, pdpUpdate *model.PdpUpdate, respMessage string) error { diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 92b715c..23f9cfe 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -25,7 +25,9 @@ import ( "github.com/go-playground/validator/v10" "github.com/google/uuid" "os" + "os/exec" "path/filepath" + "policy-opa-pdp/consts" "policy-opa-pdp/pkg/log" "policy-opa-pdp/pkg/model" "regexp" @@ -294,3 +296,25 @@ func IsValidString(name *string) bool { return true } } + +func BuildBundle(cmdFunc func(string, ...string) *exec.Cmd) (string, error) { + cmd := cmdFunc( + consts.Opa, + consts.BuildBundle, + consts.V1_COMPATIBLE, + consts.Policies, + consts.Data, + consts.Output, + consts.BundleTarGzFile, + ) + log.Debugf("Before calling combinedoutput") + output, err := cmd.CombinedOutput() + + if err != nil { + log.Warnf("Error output : %s", string(output)) + log.Warnf("Failed to build Bundle: %v", err) + return string(output), err + } + log.Debug("Bundle Built Sucessfully....") + return string(output), nil +} diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index db20148..3a4948d 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -22,12 +22,29 @@ import ( "github.com/google/uuid" "github.com/stretchr/testify/assert" "os" + "os/exec" "path/filepath" "policy-opa-pdp/pkg/model" "testing" "time" ) +func mockCmd(command string, args ...string) *exec.Cmd { + cs := []string{"-test.run=TestHelperProcess", "--", command} + cs = append(cs, args...) + cmd := exec.Command(os.Args[0], cs...) + cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"} + return cmd +} + +// TestHelperProcess is a helper process used by mockCmd +func TestHelperProcess(*testing.T) { + if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { + return + } + os.Exit(0) +} + // Positive Test Case: Valid UUIDs func TestIsValidUUIDPositive(t *testing.T) { // Define valid UUID strings @@ -585,3 +602,27 @@ func TestIsValidString(t *testing.T) { } } } + +func TestBuildBundle(t *testing.T) { + output, err := BuildBundle(mockCmd) + if err != nil { + t.Errorf("BuildBundle() error = %v, wantErr %v", err, output) + } +} + +func TestBuildBundle_CommandFailure(t *testing.T) { + // Mock function to simulate command failure + mockCmdFail := func(command string, args ...string) *exec.Cmd { + cs := []string{"-test.run=TestHelperProcess", "--", command} + cs = append(cs, args...) + cmd := exec.Command(os.Args[0], cs...) + cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"} + cmd.Stderr = os.Stderr + return cmd + } + + output, err := BuildBundle(mockCmdFail) + if err == nil { + t.Errorf("BuildBundle() error = nil, wantErr %v", output) + } +} diff --git a/test/config.json b/test/config.json index 3f2aa43..0eb38c9 100644 --- a/test/config.json +++ b/test/config.json @@ -2,22 +2,6 @@ "logging": { "level": "debug" }, - "services": [ - { - "name": "opa-bundle-server", - "url": "http://localhost:8282/opa/bundles" - } - ], - "bundles": { - "opabundle": { - "service": "opa-bundle-server", - "resource": "bundle.tar.gz", - "polling": { - "min_delay_seconds": 60, - "max_delay_seconds": 120 - } - } - }, "decision_logs": { "console": true } diff --git a/test/config/opa-pdp/config.json b/test/config/opa-pdp/config.json index 3f2aa43..0eb38c9 100644 --- a/test/config/opa-pdp/config.json +++ b/test/config/opa-pdp/config.json @@ -2,22 +2,6 @@ "logging": { "level": "debug" }, - "services": [ - { - "name": "opa-bundle-server", - "url": "http://localhost:8282/opa/bundles" - } - ], - "bundles": { - "opabundle": { - "service": "opa-bundle-server", - "resource": "bundle.tar.gz", - "polling": { - "min_delay_seconds": 60, - "max_delay_seconds": 120 - } - } - }, "decision_logs": { "console": true }