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

Feature/add mtls cert binds #2394

Open
wants to merge 3 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
3 changes: 3 additions & 0 deletions .changelog/2394.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
workers: Add mTLS cert binding for Cloudflare Workers
```
29 changes: 29 additions & 0 deletions workers_bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const (
DispatchNamespaceBindingType WorkerBindingType = "dispatch_namespace"
// WorkerD1DataseBindingType is for D1 databases.
WorkerD1DataseBindingType WorkerBindingType = "d1"
// WorkerMtlsCertificateBindingType is for mtls certificates.
WorkerMtlsCertificateBindingType WorkerBindingType = "mtls_certificate"
)

type ListWorkerBindingsParams struct {
Expand Down Expand Up @@ -433,6 +435,28 @@ func (b WorkerD1DatabaseBinding) serialize(bindingName string) (workerBindingMet
}, nil, nil
}

// WorkerMtlsCertificateBinding is a binding to a mtls certificate.
type WorkerMtlsCertificateBinding struct {
CertificateID string
}

// Type returns the type of the binding.
func (b WorkerMtlsCertificateBinding) Type() WorkerBindingType {
return WorkerMtlsCertificateBindingType
}

func (b WorkerMtlsCertificateBinding) serialize(bindingName string) (workerBindingMeta, workerBindingBodyWriter, error) {
if b.CertificateID == "" {
return nil, nil, fmt.Errorf(`certificate ID for binding "%s" cannot be empty`, bindingName)
}

return workerBindingMeta{
"name": bindingName,
"type": b.Type(),
"id": b.CertificateID,
}, nil, nil
}

// UnsafeBinding is for experimental or deprecated bindings, and allows specifying any binding type or property.
type UnsafeBinding map[string]interface{}

Expand Down Expand Up @@ -562,6 +586,11 @@ func (api *API) ListWorkerBindings(ctx context.Context, rc *ResourceContainer, p
bindingListItem.Binding = WorkerD1DatabaseBinding{
DatabaseID: database_id,
}
case WorkerMtlsCertificateBindingType:
certificate_id := jsonBinding["certificate_id"].(string)
bindingListItem.Binding = WorkerMtlsCertificateBinding{
CertificateID: certificate_id,
}
default:
bindingListItem.Binding = WorkerInheritBinding{}
}
Expand Down
22 changes: 19 additions & 3 deletions workers_bindings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestListWorkerBindings(t *testing.T) {
assert.NoError(t, err)

assert.Equal(t, successResponse, res.Response)
assert.Equal(t, 9, len(res.BindingList))
assert.Equal(t, 10, len(res.BindingList))

assert.Equal(t, res.BindingList[0], WorkerBindingListItem{
Name: "MY_KV",
Expand Down Expand Up @@ -106,6 +106,15 @@ func TestListWorkerBindings(t *testing.T) {
},
})
assert.Equal(t, WorkerD1DataseBindingType, res.BindingList[8].Binding.Type())

assert.Equal(t, res.BindingList[9], WorkerBindingListItem{
Name: "MY_CERTIFICATE",
Binding: WorkerMtlsCertificateBinding{
CertificateID: "e0eeaf74-279a-45e3-8d27-65f336b94161",
},
})

assert.Equal(t, WorkerMtlsCertificateBindingType, res.BindingList[9].Binding.Type())
}

func TestListWorkerBindings_Wfp(t *testing.T) {
Expand All @@ -125,7 +134,7 @@ func TestListWorkerBindings_Wfp(t *testing.T) {
assert.NoError(t, err)

assert.Equal(t, successResponse, res.Response)
assert.Equal(t, 9, len(res.BindingList))
assert.Equal(t, 10, len(res.BindingList))

assert.Equal(t, res.BindingList[0], WorkerBindingListItem{
Name: "MY_KV",
Expand Down Expand Up @@ -181,7 +190,6 @@ func TestListWorkerBindings_Wfp(t *testing.T) {
Dataset: "my_dataset",
},
})

assert.Equal(t, WorkerAnalyticsEngineBindingType, res.BindingList[7].Binding.Type())

assert.Equal(t, res.BindingList[8], WorkerBindingListItem{
Expand All @@ -191,6 +199,14 @@ func TestListWorkerBindings_Wfp(t *testing.T) {
},
})
assert.Equal(t, WorkerD1DataseBindingType, res.BindingList[8].Binding.Type())

assert.Equal(t, res.BindingList[9], WorkerBindingListItem{
Name: "MY_CERTIFICATE",
Binding: WorkerMtlsCertificateBinding{
CertificateID: "e0eeaf74-279a-45e3-8d27-65f336b94161",
},
})
assert.Equal(t, WorkerMtlsCertificateBindingType, res.BindingList[9].Binding.Type())
}

func ExampleUnsafeBinding() {
Expand Down
5 changes: 5 additions & 0 deletions workers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ const (
"name": "MY_DATABASE",
"type": "d1",
"database_id": "cef5331f-e5c7-4c8a-a415-7908ae45f92a"
},
{
"name": "MY_CERTIFICATE",
"type": "mtls_certificate",
"certificate_id": "e0eeaf74-279a-45e3-8d27-65f336b94161"
}
],
"success": true,
Expand Down