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

Fix rest API connection CC to enforcer #2690

Merged
merged 2 commits into from
Jan 20, 2025
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
9 changes: 5 additions & 4 deletions gateway/enforcer/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"time"

"github.com/wso2/apk/gateway/enforcer/internal/config"
"github.com/wso2/apk/gateway/enforcer/internal/datastore"
"github.com/wso2/apk/gateway/enforcer/internal/extproc"
"github.com/wso2/apk/gateway/enforcer/internal/grpc"
"github.com/wso2/apk/gateway/enforcer/internal/util"
"github.com/wso2/apk/gateway/enforcer/internal/xds"
"github.com/wso2/apk/gateway/enforcer/internal/extproc"
)

func main() {
Expand All @@ -27,13 +28,13 @@ func main() {

//Create the TLS configuration
tlsConfig := util.CreateTLSConfig(clientCert, certPool)
client := grpc.NewEventingGRPCClient(host, port, cfg.XdsMaxRetries, time.Duration(cfg.XdsRetryPeriod)*time.Millisecond, tlsConfig, cfg, nil)
client := grpc.NewEventingGRPCClient(host, port, cfg.XdsMaxRetries, time.Duration(cfg.XdsRetryPeriod)*time.Second, tlsConfig, cfg, datastore.NewDataStore(cfg))
// Start the connection
client.InitiateEventingGRPCConnection()

// Create the XDS clients
apiStore, _,_ := xds.CreateXDSClients(cfg)
apiStore, _, _ := xds.CreateXDSClients(cfg)

// Start the external processing server
go extproc.StartExternalProcessingServer(cfg, apiStore)

Expand Down
52 changes: 48 additions & 4 deletions gateway/enforcer/internal/datastore/subs_app_datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
package datastore

import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"sync"

subscription_model "github.com/wso2/apk/common-go-libs/pkg/server/model"
Expand Down Expand Up @@ -214,7 +216,12 @@ func (ds *SubscriptionApplicationDataStore) LoadStartupData() error {
// Get all applications
func (ds *SubscriptionApplicationDataStore) getAllApplications() (*subscription_model.ApplicationList, error) {
url := fmt.Sprintf("%s/applications", ds.commonControllerRestBaseURL)
resp, err := util.MakeGETRequest(url)
// Get the TLS configuration
tlsConfig, err := GetTLSConfig()
if err != nil {
return nil, fmt.Errorf("failed to get TLS config: %w", err)
}
resp, err := util.MakeGETRequest(url, tlsConfig)
if err != nil {
return nil, err
}
Expand All @@ -225,13 +232,20 @@ func (ds *SubscriptionApplicationDataStore) getAllApplications() (*subscription_
if err := json.Unmarshal(body, &result); err != nil {
return nil, err
}
log.Println("Applications: ", result)
return &result, nil
}

// Get all subscriptions
func (ds *SubscriptionApplicationDataStore) getAllSubscriptions() (*subscription_model.SubscriptionList, error) {
url := fmt.Sprintf("%s/subscriptions", ds.commonControllerRestBaseURL)
resp, err := util.MakeGETRequest(url)
// Get the TLS configuration
tlsConfig, err := GetTLSConfig()
if err != nil {
return nil, fmt.Errorf("failed to get TLS config: %w", err)
}
resp, err := util.MakeGETRequest(url, tlsConfig)
log.Println("Response: ", resp)
if err != nil {
return nil, err
}
Expand All @@ -242,13 +256,18 @@ func (ds *SubscriptionApplicationDataStore) getAllSubscriptions() (*subscription
if err := json.Unmarshal(body, &result); err != nil {
return nil, err
}
log.Println("Subscription: ", result)
return &result, nil
}

// Get all application mappings
func (ds *SubscriptionApplicationDataStore) getAllApplicationMappings() (*subscription_model.ApplicationMappingList, error) {
url := fmt.Sprintf("%s/applicationmappings", ds.commonControllerRestBaseURL)
resp, err := util.MakeGETRequest(url)
tlsConfig, err := GetTLSConfig()
if err != nil {
return nil, fmt.Errorf("failed to get TLS config: %w", err)
}
resp, err := util.MakeGETRequest(url, tlsConfig)
if err != nil {
return nil, err
}
Expand All @@ -265,7 +284,11 @@ func (ds *SubscriptionApplicationDataStore) getAllApplicationMappings() (*subscr
// Get all application key mappings
func (ds *SubscriptionApplicationDataStore) getAllApplicationKeyMappings() (*subscription_model.ApplicationKeyMappingList, error) {
url := fmt.Sprintf("%s/applicationkeymappings", ds.commonControllerRestBaseURL)
resp, err := util.MakeGETRequest(url)
tlsConfig, err := GetTLSConfig()
if err != nil {
return nil, fmt.Errorf("failed to get TLS config: %w", err)
}
resp, err := util.MakeGETRequest(url, tlsConfig)
if err != nil {
return nil, err
}
Expand All @@ -278,3 +301,24 @@ func (ds *SubscriptionApplicationDataStore) getAllApplicationKeyMappings() (*sub
}
return &result, nil
}

// GetTLSConfig loads and returns a TLS configuration
func GetTLSConfig() (*tls.Config, error) {
cfg := config.GetConfig()

// Load the client certificate and private key
clientCert, err := util.LoadCertificates(cfg.EnforcerPublicKeyPath, cfg.EnforcerPrivateKeyPath)
if err != nil {
return nil, fmt.Errorf("failed to load client certificate and private key: %w", err)
}

// Load the trusted CA certificates
certPool, err := util.LoadCACertificates(cfg.TrustedAdapterCertsPath)
if err != nil {
return nil, fmt.Errorf("failed to load trusted CA certificates: %w", err)
}

// Create and return the TLS configuration
tlsConfig := util.CreateTLSConfig(clientCert, certPool)
return tlsConfig, nil
}
5 changes: 3 additions & 2 deletions gateway/enforcer/internal/grpc/eventing_grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,13 @@ func (c *EventingGRPCClient) InitiateEventingGRPCConnection() {
return
}
c.log.Info(fmt.Sprintf("Received config: %v", resp))
c.HandleNotificationEvent(resp)
}
}()
}

// handleNotificationEvent translates the Java method to Go
func (c *EventingGRPCClient) handleNotificationEvent(event *subscription_proto_model.Event) {
// HandleNotificationEvent translates the Java method to Go
func (c *EventingGRPCClient) HandleNotificationEvent(event *subscription_proto_model.Event) {
switch event.Type {
case "ALL_EVENTS":
log.Println("Received all events from the server")
Expand Down
19 changes: 16 additions & 3 deletions gateway/enforcer/internal/util/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,30 @@
package util

import (
"crypto/tls"
"log"
"net/http"
)

// MakeGETRequest HTTP client for making GET requests
func MakeGETRequest(url string) (*http.Response, error) {
client := &http.Client{}
// MakeGETRequest HTTP client for making GET requests with custom TLS config
func MakeGETRequest(url string, tlsConfig *tls.Config) (*http.Response, error) {
// Create a custom HTTP client with the provided TLS configuration
tr := &http.Transport{
TLSClientConfig: tlsConfig,
}
client := &http.Client{Transport: tr}

// Create the HTTP request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}

// Set request headers
req.Header.Set("Content-Type", "application/json")

log.Println("GET Request: ", req)

// Execute the request
return client.Do(req)
}
Loading