Skip to content

Commit

Permalink
adding test clients
Browse files Browse the repository at this point in the history
  • Loading branch information
vramk23 committed Aug 8, 2023
1 parent b1532a4 commit 1112790
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 41 deletions.
41 changes: 0 additions & 41 deletions server/examples/agent.go

This file was deleted.

62 changes: 62 additions & 0 deletions server/examples/agent/agent_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"context"
"fmt"
"os"

"github.com/intelops/go-common/credentials"
"github.com/intelops/go-common/logging"
"github.com/kube-tarian/kad/server/pkg/agent"
"github.com/kube-tarian/kad/server/pkg/pb/agentpb"
)

func main() {
log := logging.NewLogger()
cfg, err := getAgentConfig("https://captenagent.dev.test.app")
if err != nil {
log.Fatalf("failed to load agent config: ", err)
}

ac, err := agent.NewAgent(log, cfg)
if err != nil {
log.Fatalf("failed to connect to agent: ", err)
return
}
storeServiceCred(ac.GetClient())
}

func storeServiceCred(ac agentpb.AgentClient) {
serviceCred := credentials.ServiceCredential{
UserName: "testuser",
Password: "password2",
}
serviceCredMap := credentials.PrepareServiceCredentialMap(serviceCred)
_, err := ac.StoreCredential(context.Background(), &agentpb.StoreCredentialRequest{
CredentialType: credentials.ServiceUserCredentialType,
CredEntityName: "testentity",
CredIdentifier: "testentityuser",
Credential: serviceCredMap,
})
if err != nil {
fmt.Println("store error: ", err)
return
}
fmt.Println("successful")
}

func getAgentConfig(address string) (*agent.Config, error) {
cadata, err := os.ReadFile("/var/capten/cert/ca.crt")
if err != nil {
return nil, fmt.Errorf("ca failed, %v", err)
}
cdata, err := os.ReadFile("/var/capten/cert/client.crt")
if err != nil {
return nil, fmt.Errorf("client failed, %v", err)
}
ckey, err := os.ReadFile("/var/capten/cert/client.key")
if err != nil {
return nil, fmt.Errorf("key failed, %v", err)
}
return &agent.Config{Address: address, CaCert: string(cadata), Cert: string(cdata), Key: string(ckey)}, nil
}
46 changes: 46 additions & 0 deletions server/examples/astra/astra-client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"crypto/tls"
"log"

"github.com/stargate/stargate-grpc-go-client/stargate/pkg/auth"
"github.com/stargate/stargate-grpc-go-client/stargate/pkg/client"
"github.com/stargate/stargate-grpc-go-client/stargate/pkg/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)

func main() {
host := "<DBID>-<region>.apps.astra.datastax.com:443"
token := "AstraCS:"
tlsConfig := &tls.Config{
InsecureSkipVerify: false,
}

log.Printf("connecting to astra %s", host)
conn, err := grpc.Dial(host, grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)),
grpc.WithBlock(),
grpc.WithPerRPCCredentials(
auth.NewStaticTokenProvider(token),
),
)
if err != nil {
log.Fatalf("failed to connect to astra db, %v", err)
}

log.Printf("connected to %v", conn)
session, err := client.NewStargateClientWithConn(conn)
if err != nil {
log.Fatalf("error creating stargate client, %v", err)
}

log.Printf("exec query")
res, err := session.ExecuteQuery(&proto.Query{Cql: "DESCRIBE 'capten'"})
if err != nil {
log.Fatalf("error exec query, %v", err)
}

result := res.GetResult()
log.Printf("result, %v", result)
}
121 changes: 121 additions & 0 deletions server/examples/server/server_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package main

import (
"context"
"fmt"
"os"

"github.com/kube-tarian/kad/server/pkg/pb/serverpb"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
)

func main() {
fmt.Println("Registration testing")
gr, err := grpc.Dial("captenserver.test.app:80", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
fmt.Println("GRPC failed", err)
return
}
sc := serverpb.NewServerClient(gr)
ctx := context.TODO()
ctx = metadata.AppendToOutgoingContext(ctx, "organizationID", "eab9af58-1cbd-4f25-b3f3-d07ee99a5baa")

resp1, err := sc.GetClusters(ctx, &serverpb.GetClustersRequest{})
if err != nil {
fmt.Println("error with get registrations ", err)
} else {
fmt.Println("registrations fetch done", resp1.Status, resp1.StatusMessage, len(resp1.Data))
for _, cluster := range resp1.Data {
fmt.Printf("cluster: %+v\n", *cluster)
}
}

cadata, err := os.ReadFile("/var/capten/cert/ca.crt")
if err != nil {
fmt.Println("ca failed", err)
return
}

cdata, err := os.ReadFile("/var/capten/cert/client.crt")
if err != nil {
fmt.Println("client failed", err)
return
}

ckey, err := os.ReadFile("/var/dev/capten/cert/client.key")
if err != nil {
fmt.Println("key failed", err)
return
}

nresp, err := sc.NewClusterRegistration(ctx, &serverpb.NewClusterRegistrationRequest{
AgentEndpoint: "https://captenagent.dev.test.app",
ClusterName: "awscluster-3",
ClientKeyData: string(ckey),
ClientCertData: string(cdata),
ClientCAChainData: string(cadata),
})
if err != nil {
fmt.Println("error with registration ", err)
} else {
fmt.Println("Server testing done", nresp.Status, nresp.StatusMessage, nresp.ClusterID)
}

nresp1, err := sc.NewClusterRegistration(ctx, &serverpb.NewClusterRegistrationRequest{
AgentEndpoint: "https://captenagent.dev.test.app",
ClusterName: "awscluster-4",
ClientKeyData: "fsdfgsdfsdf",
ClientCertData: "bcvbvcbhhhfh",
ClientCAChainData: "tyrytertyeyeyey",
})
if err != nil {
fmt.Println("error with registration ", err)
} else {
fmt.Println("Server testing done", nresp1.Status, nresp1.StatusMessage, nresp1.ClusterID)
}

gresp2, err := sc.GetClusters(ctx, &serverpb.GetClustersRequest{})
if err != nil {
fmt.Println("error with get registrations ", err)
} else {
fmt.Println("registrations fetch done", gresp2.Status, gresp2.StatusMessage, len(gresp2.Data))
for _, cluster := range gresp2.Data {
fmt.Printf("cluster: %+v\n", *cluster)
}
}

uresp, err := sc.UpdateClusterRegistration(ctx, &serverpb.UpdateClusterRegistrationRequest{
ClusterID: nresp.ClusterID,
AgentEndpoint: "https://captenagent.dev.test.app1",
ClusterName: "awscluster-3-update",
ClientKeyData: string(ckey),
ClientCertData: string(cdata),
ClientCAChainData: string(cadata),
})
if err != nil {
fmt.Println("error with get registrations ", err)
} else {
fmt.Println("registrations fetch done", uresp.Status, uresp.StatusMessage)
}

gresp3, err := sc.GetClusters(ctx, &serverpb.GetClustersRequest{})
if err != nil {
fmt.Println("error with get registrations ", err)
} else {
fmt.Println("registrations fetch done", gresp3.Status, gresp3.StatusMessage, len(gresp3.Data))
for _, cluster := range gresp3.Data {
fmt.Printf("cluster: %+v\n", *cluster)
}
}

for _, cluster := range gresp3.Data {
dresp, err := sc.DeleteClusterRegistration(ctx, &serverpb.DeleteClusterRegistrationRequest{ClusterID: cluster.ClusterID})
if err != nil {
fmt.Println("error with get registrations ", err, cluster.ClusterID)
} else {
fmt.Println("registrations delete done", dresp.Status, dresp.StatusMessage, cluster.ClusterID)
}
}
}

0 comments on commit 1112790

Please sign in to comment.