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

export events #235

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions etc/export-events.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"index_patterns": ["export_events*"], // Pattern to match the names of indices that should use this template
"settings": {
"number_of_shards": 1, // Adjust based on expected load and data volume
"number_of_replicas": 1, // Can be adjusted for high availability
"refresh_interval": "5s" // Adjust based on write and query performance needs
},
"mappings": {
"properties": {
"project_id": {
"type": "keyword" // Using keyword type for exact value matching and aggregations
},
"enabled": {
"type": "boolean" // Boolean type for enabled/disabled status
},
"bucket_name": {
"type": "keyword" // Using keyword to facilitate exact matches
},
"last_run_time": {
"type": "date", // Date type for timestamping the last run
"format": "epoch_millisecond" // Storing timestamp in milliseconds since the epoch
},
"filters": {
"type": "object", // Object type to nest filter configurations
"properties": {
"event_types": {
"type": "keyword" // Array of keywords for event types
},
"resource_types": {
"type": "keyword" // Array of keywords for resource types
},
"exclude_event_types": {
"type": "keyword" // Array of keywords for excluded event types
},
"exclude_resource_types": {
"type": "keyword" // Array of keywords for excluded resource types
}
}
}
}
}
}
1 change: 1 addition & 0 deletions etc/hermes.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ PolicyFilePath = "etc/policy.json"

[elasticsearch]
url = "http://localhost:9200"
ElasticTemplatePath = "etc/export_events.tpl"

[keystone]
auth_url = "https://identity-3.staging.cloud.sap/v3/"
Expand Down
34 changes: 34 additions & 0 deletions pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package api
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"

Expand Down Expand Up @@ -88,3 +89,36 @@ func Test_API(t *testing.T) {
})
}
}

func TestExportEvents(t *testing.T) {
router := setupTest(t)

req := httptest.NewRequest(http.MethodGet, "/v1/export-events?project_id=test-project", http.NoBody)
req.Header.Set("X-Auth-Token", "test-token")

recorder := httptest.NewRecorder()
router.ServeHTTP(recorder, req)

if recorder.Code != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, recorder.Code)
}

var response struct {
Message string `json:"message"`
ProjectID string `json:"project_id"`
}
err := json.Unmarshal(recorder.Body.Bytes(), &response)
if err != nil {
t.Fatalf("Failed to parse JSON response: %v", err)
}

expectedMessage := "Export Events feature is being implemented"
if response.Message != expectedMessage {
t.Errorf("Expected message '%s', got '%s'", expectedMessage, response.Message)
}

expectedProjectID := "test-project"
if response.ProjectID != expectedProjectID {
t.Errorf("Expected project_id '%s', got '%s'", expectedProjectID, response.ProjectID)
}
}
2 changes: 2 additions & 0 deletions pkg/api/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ func NewV1Handler(validator gopherpolicy.Validator, storageInterface storage.Sto
observeDuration(observeResponseSize(p.GetEventDetails, "GetEventDetails"), "GetEventDetails"))
r.Methods("GET").Path("/v1/attributes/{attribute_name}").HandlerFunc(
observeDuration(observeResponseSize(p.GetAttributes, "GetAttributes"), "GetAttributes"))
r.Methods("GET").Path("/v1/export-events").HandlerFunc(
observeDuration(observeResponseSize(p.ExportEvents, "ExportEvents"), "ExportEvents"))

return r, p.versionData
}
Expand Down
28 changes: 27 additions & 1 deletion pkg/api/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/pkg/errors"
"github.com/sapcc/go-bits/gopherpolicy"

Check failure on line 35 in pkg/api/events.go

View workflow job for this annotation

GitHub Actions / Checks

"github.com/sapcc/go-bits/gopherpolicy" imported and not used

Check failure on line 35 in pkg/api/events.go

View workflow job for this annotation

GitHub Actions / Checks

"github.com/sapcc/go-bits/gopherpolicy" imported and not used

Check failure on line 35 in pkg/api/events.go

View workflow job for this annotation

GitHub Actions / CodeQL

"github.com/sapcc/go-bits/gopherpolicy" imported and not used

Check failure on line 35 in pkg/api/events.go

View workflow job for this annotation

GitHub Actions / Build

"github.com/sapcc/go-bits/gopherpolicy" imported and not used
"github.com/sapcc/go-bits/logg"

"github.com/sapcc/hermes/pkg/hermes"
Expand Down Expand Up @@ -336,7 +336,33 @@
ReturnJSON(res, http.StatusOK, attribute)
}

func getIndexID(token *gopherpolicy.Token, r *http.Request, w http.ResponseWriter) (string, error) {
func (p *v1Provider) ExportEvents(w http.ResponseWriter, r *http.Request) {
// Check if the user has permission to export events
token := p.CheckToken(r)

Check failure on line 341 in pkg/api/events.go

View workflow job for this annotation

GitHub Actions / Checks

p.CheckToken undefined (type *v1Provider has no field or method CheckToken)

Check failure on line 341 in pkg/api/events.go

View workflow job for this annotation

GitHub Actions / Checks

p.CheckToken undefined (type *v1Provider has no field or method CheckToken)

Check failure on line 341 in pkg/api/events.go

View workflow job for this annotation

GitHub Actions / CodeQL

p.CheckToken undefined (type *v1Provider has no field or method CheckToken)

Check failure on line 341 in pkg/api/events.go

View workflow job for this annotation

GitHub Actions / Build

p.CheckToken undefined (type *v1Provider has no field or method CheckToken)
if !token.Require(w, "event:export") {
logg.Error("Permission denied for event:export")
return
}

// Parse query parameters
projectID := r.URL.Query().Get("project_id")
if projectID == "" {
http.Error(w, "project_id is required", http.StatusBadRequest)
return
}

// For now, just return a JSON response indicating the feature is in progress
response := struct {
Message string `json:"message"`
ProjectID string `json:"project_id"`
}{
Message: "Export Events feature is being implemented",
ProjectID: projectID,
}
ReturnJSON(w, http.StatusOK, response)
}

func getIndexID(token *Token, r *http.Request, w http.ResponseWriter) (string, error) {

Check failure on line 365 in pkg/api/events.go

View workflow job for this annotation

GitHub Actions / Checks

undefined: Token) (typecheck)

Check failure on line 365 in pkg/api/events.go

View workflow job for this annotation

GitHub Actions / Checks

undefined: Token (typecheck)

Check failure on line 365 in pkg/api/events.go

View workflow job for this annotation

GitHub Actions / CodeQL

undefined: Token

Check failure on line 365 in pkg/api/events.go

View workflow job for this annotation

GitHub Actions / Build

undefined: Token
// Get index ID from a token
// Defaults to a token project scope
indexID := token.Context.Auth["project_id"]
Expand Down
4 changes: 4 additions & 0 deletions pkg/api/fixtures/export-events.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"message": "Export Events feature is being implemented",
"project_id": "test-project"
}
35 changes: 35 additions & 0 deletions pkg/storage/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"fmt"
"log"
"math"
"os"
"strings"

elastic "github.com/olivere/elastic/v7"
Expand Down Expand Up @@ -87,6 +88,11 @@ func (es *ElasticSearch) init() {
// If issues - https://github.com/olivere/elastic/wiki/Connection-Problems#how-to-figure-out-connection-problems
panic(err)
}

err = es.EnsureIndexTemplate()
if err != nil {
logg.Error("Failed to ensure index template: %v", err)
}
}

// Mapping for attributes based on return values to API
Expand Down Expand Up @@ -361,3 +367,32 @@ func indexName(tenantID string) string {
}
return index
}

// EnsureIndexTemplate checks if the index template exists, and if not, creates it.
func (es *ElasticSearch) EnsureIndexTemplate() error {
templateName := "export_events"
templatePath := viper.GetString("ElasticTemplatePath")

templateContent, err := os.ReadFile(templatePath)
if err != nil {
return fmt.Errorf("failed to read index template file: %w", err)
}

// Initialize the index template service
service := elastic.NewIndicesPutIndexTemplateService(es.client())
service.Name(templateName).BodyString(string(templateContent))

// Execute the request to create or update the index template
response, err := service.Do(context.Background())
if err != nil {
return fmt.Errorf("failed to create or update the index template: %w", err)
}

if response.Acknowledged {
logg.Info("Index template created or updated successfully")
} else {
logg.Info("Index template creation or update not acknowledged")
}

return nil
}
3 changes: 2 additions & 1 deletion pkg/test/policy.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"event:list": "@",
"event:show": "@"
"event:show": "@",
"event:export": "@"
}
Loading