-
Notifications
You must be signed in to change notification settings - Fork 100
feat: TLS Helloworld sample for gRPC calls to cadence #114
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
Merged
+253
−2
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5bc024b
TLS Helloworld sample for gRPC calls to cadence
vishwa-uber 70cbd1d
TLS Helloworld sample for gRPC calls to cadence: fix
vishwa-uber 1e1e1e7
feat: update the read me with server Pre-requisites
vishwa-uber d8b48e2
feat: update the read me with server Pre-requisites
vishwa-uber 4991fab
Accomodating review comments
vishwa-uber 1a12e09
Accomodating review comments
vishwa-uber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
## Pre-requisites | ||
|
||
Follow this document to start cadence server: | ||
https://github.com/cadence-workflow/cadence/blob/e1267de12f8bc670fc84fab456d3495c8fc2f8a8/CONTRIBUTING.md#L1 | ||
|
||
1. **Build tools in cadence server** | ||
```bash | ||
make bins | ||
``` | ||
|
||
2. **Start cassandra** | ||
```bash | ||
docker compose -f ./docker/dev/cassandra.yml up -d | ||
``` | ||
|
||
3. **Install schema** | ||
```bash | ||
make install-schema | ||
``` | ||
|
||
4. **Start cadence server with TLS** | ||
```bash | ||
./cadence-server --env development --zone tls start | ||
``` | ||
|
||
## Running the Sample | ||
|
||
### Step 1: Download Certificates | ||
```bash | ||
cd new_samples/client_samples/helloworld_tls/credentials | ||
Download certificates from config/credentials of cadence server and place them here | ||
vishwa-uber marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
cd .. | ||
``` | ||
|
||
### Step 2: Register the Domain | ||
Before running workflows, you must register the "default" domain: | ||
|
||
```bash | ||
cd new_samples/client_samples/helloworld_tls | ||
go run register_domain.go | ||
``` | ||
|
||
Expected output: | ||
``` | ||
Successfully registered domain {"domain": "default"} | ||
``` | ||
|
||
If the domain already exists, you'll see: | ||
``` | ||
Domain already exists {"domain": "default"} | ||
``` | ||
|
||
### Step 3: Run the Sample | ||
In another terminal: | ||
```bash | ||
cd new_samples/client_samples/helloworld_tls | ||
go run hello_world_tls.go | ||
``` | ||
|
||
## References | ||
|
||
- [Cadence Official Certificates](https://github.com/cadence-workflow/cadence/tree/master/config/credentials) | ||
- [Cadence Documentation](https://cadenceworkflow.io/) | ||
- [Go TLS Package](https://pkg.go.dev/crypto/tls) | ||
|
86 changes: 86 additions & 0 deletions
86
new_samples/client_samples/helloworld_tls/hello_world_tls.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/uber-common/cadence-samples/new_samples/worker" | ||
"go.uber.org/cadence/.gen/go/shared" | ||
"go.uber.org/yarpc/transport/grpc" | ||
"go.uber.org/zap" | ||
"google.golang.org/grpc/credentials" | ||
) | ||
|
||
func main() { | ||
withTLSDialOption, err := withTLSDialOption() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
cadenceClient := worker.BuildCadenceClient(withTLSDialOption) | ||
logger := worker.BuildLogger() | ||
|
||
domain := "default" | ||
tasklist := "default-tasklist" | ||
workflowID := uuid.New().String() | ||
requestID := uuid.New().String() | ||
executionTimeout := int32(60) | ||
closeTimeout := int32(60) | ||
|
||
workflowType := "cadence_samples.HelloWorldWorkflow" | ||
input := []byte(`{"message": "Uber"}`) | ||
|
||
req := shared.StartWorkflowExecutionRequest{ | ||
Domain: &domain, | ||
WorkflowId: &workflowID, | ||
WorkflowType: &shared.WorkflowType{ | ||
Name: &workflowType, | ||
}, | ||
TaskList: &shared.TaskList{ | ||
Name: &tasklist, | ||
}, | ||
Input: input, | ||
ExecutionStartToCloseTimeoutSeconds: &executionTimeout, | ||
TaskStartToCloseTimeoutSeconds: &closeTimeout, | ||
RequestId: &requestID, | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute) | ||
defer cancel() | ||
resp, err := cadenceClient.StartWorkflowExecution(ctx, &req) | ||
if err != nil { | ||
logger.Error("Failed to create workflow", zap.Error(err)) | ||
panic("Failed to create workflow.") | ||
} | ||
|
||
logger.Info("successfully started HelloWorld workflow", zap.String("runID", resp.GetRunId())) | ||
} | ||
|
||
func withTLSDialOption() (grpc.DialOption, error) { | ||
// Present client cert for mutual TLS (if enabled on server) | ||
clientCert, err := tls.LoadX509KeyPair("credentials/client.crt", "credentials/client.key") | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to load client certificate: %v", zap.Error(err)) | ||
} | ||
|
||
// Load server CA | ||
caCert, err := os.ReadFile("credentials/keytest.crt") | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to load server CA certificate: %v", zap.Error(err)) | ||
} | ||
caCertPool := x509.NewCertPool() | ||
caCertPool.AppendCertsFromPEM(caCert) | ||
tlsConfig := tls.Config{ | ||
InsecureSkipVerify: true, | ||
RootCAs: caCertPool, | ||
Certificates: []tls.Certificate{clientCert}, | ||
} | ||
creds := credentials.NewTLS(&tlsConfig) | ||
grpc.DialerCredentials(creds) | ||
return grpc.DialerCredentials(creds), nil | ||
} |
88 changes: 88 additions & 0 deletions
88
new_samples/client_samples/helloworld_tls/register_domain.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/uber-common/cadence-samples/new_samples/worker" | ||
"go.uber.org/cadence/.gen/go/shared" | ||
"go.uber.org/yarpc/transport/grpc" | ||
"go.uber.org/zap" | ||
"google.golang.org/grpc/credentials" | ||
) | ||
|
||
func main() { | ||
logger := worker.BuildLogger() | ||
logger.Info("Registering default domain for cadence-vishwa with TLS...") | ||
|
||
withTLSDialOption, err := buildTLSDialOption() | ||
if err != nil { | ||
logger.Fatal("Failed to build TLS dial option", zap.Error(err)) | ||
} | ||
|
||
cadenceClient := worker.BuildCadenceClient(withTLSDialOption) | ||
|
||
// Register the domain | ||
domain := "default" | ||
retentionDays := int32(7) | ||
emitMetric := true | ||
|
||
req := &shared.RegisterDomainRequest{ | ||
Name: &domain, | ||
Description: stringPtr("Default domain for cadence samples"), | ||
WorkflowExecutionRetentionPeriodInDays: &retentionDays, | ||
EmitMetric: &emitMetric, | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
|
||
err = cadenceClient.RegisterDomain(ctx, req) | ||
if err != nil { | ||
// Check if domain already exists | ||
if _, ok := err.(*shared.DomainAlreadyExistsError); ok { | ||
logger.Info("Domain already exists", zap.String("domain", domain)) | ||
return | ||
} | ||
logger.Fatal("Failed to register domain", zap.Error(err)) | ||
} | ||
|
||
logger.Info("Successfully registered domain", zap.String("domain", domain)) | ||
} | ||
|
||
func buildTLSDialOption() (grpc.DialOption, error) { | ||
// Load client certificate | ||
clientCert, err := tls.LoadX509KeyPair("credentials/client.crt", "credentials/client.key") | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to load client certificate: %w", err) | ||
} | ||
|
||
// Load server CA | ||
caCert, err := os.ReadFile("credentials/keytest.crt") | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to load server CA certificate: %w", err) | ||
} | ||
|
||
caCertPool := x509.NewCertPool() | ||
if !caCertPool.AppendCertsFromPEM(caCert) { | ||
return nil, fmt.Errorf("failed to append CA certificate") | ||
} | ||
|
||
tlsConfig := &tls.Config{ | ||
InsecureSkipVerify: true, | ||
RootCAs: caCertPool, | ||
Certificates: []tls.Certificate{clientCert}, | ||
MinVersion: tls.VersionTLS12, | ||
} | ||
|
||
creds := credentials.NewTLS(tlsConfig) | ||
return grpc.DialerCredentials(creds), nil | ||
} | ||
|
||
func stringPtr(s string) *string { | ||
return &s | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.