Skip to content

Commit

Permalink
Support for Secret Injections (#20)
Browse files Browse the repository at this point in the history
add support for Secret Injections
  • Loading branch information
arielsepton authored Apr 29, 2024
1 parent 9a2a4ac commit 76c63e4
Show file tree
Hide file tree
Showing 52 changed files with 3,690 additions and 273 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
workflow_dispatch:
inputs:
version:
description: 'Release version (e.g. v0.2.0)'
description: 'Release version (e.g. v1.0.0)'
required: true
message:
description: 'Tag message'
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ To install `provider-http`, you have two options:
1. Using the Crossplane CLI in a Kubernetes cluster where Crossplane is installed:

```console
kubectl crossplane install provider xpkg.upbound.io/crossplane-contrib/provider-http:v0.2.0
kubectl crossplane install provider xpkg.upbound.io/crossplane-contrib/provider-http:v1.0.0
```

2. Manually creating a Provider by applying the following YAML:
Expand Down Expand Up @@ -39,7 +39,7 @@ To install `provider-http`, you have two options:
Create a `DisposableRequest` resource to initiate a single-use HTTP interaction:

```yaml
apiVersion: http.crossplane.io/v1alpha1
apiVersion: http.crossplane.io/v1alpha2
kind: DisposableRequest
metadata:
name: example-disposable-request
Expand All @@ -54,7 +54,7 @@ For more detailed examples and configuration options, refer to the [examples dir
Manage a resource through HTTP requests with a `Request` resource:

```yaml
apiVersion: http.crossplane.io/v1alpha1
apiVersion: http.crossplane.io/v1alpha2
kind: Request
metadata:
name: example-request
Expand All @@ -64,13 +64,13 @@ spec:
For more detailed examples and configuration options, refer to the [examples directory](examples/sample/).


### Developing locally
## Developing locally

Run controller against the cluster:
```
make run
```


### Troubleshooting
## Troubleshooting
If you encounter any issues during installation or usage, refer to the [troubleshooting guide](https://docs.crossplane.io/knowledge-base/guides/troubleshoot/) for common problems and solutions.
144 changes: 144 additions & 0 deletions apis/disposablerequest/v1alpha2/disposablerequest_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
Copyright 2022 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha2

import (
"reflect"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
)

// DisposableRequestParameters are the configurable fields of a DisposableRequest.
type DisposableRequestParameters struct {
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Field 'forProvider.url' is immutable"
URL string `json:"url"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Field 'forProvider.method' is immutable"
Method string `json:"method"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Field 'forProvider.headers' is immutable"
Headers map[string][]string `json:"headers,omitempty"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Field 'forProvider.body' is immutable"
Body string `json:"body,omitempty"`

// WaitTimeout specifies the maximum time duration for waiting.
WaitTimeout *metav1.Duration `json:"waitTimeout,omitempty"`

// RollbackRetriesLimit is max number of attempts to retry HTTP request by sending again the request.
RollbackRetriesLimit *int32 `json:"rollbackRetriesLimit,omitempty"`

// InsecureSkipTLSVerify, when set to true, skips TLS certificate checks for the HTTP request
InsecureSkipTLSVerify bool `json:"insecureSkipTLSVerify,omitempty"`

// ExpectedResponse is a jq filter expression used to evaluate the HTTP response and determine if it matches the expected criteria.
// The expression should return a boolean; if true, the response is considered expected.
// Example: '.Body.job_status == "success"'
ExpectedResponse string `json:"expectedResponse,omitempty"`

// SecretInjectionConfig specifies the secrets receiving patches for response data.
SecretInjectionConfigs []SecretInjectionConfig `json:"secretInjectionConfigs,omitempty"`
}

// A DisposableRequestSpec defines the desired state of a DisposableRequest.
type DisposableRequestSpec struct {
xpv1.ResourceSpec `json:",inline"`
ForProvider DisposableRequestParameters `json:"forProvider"`
}

// SecretInjectionConfig represents the configuration for injecting secret data into a Kubernetes secret.
type SecretInjectionConfig struct {
// SecretRef contains the name and namespace of the Kubernetes secret where the data will be injected.
SecretRef SecretRef `json:"secretRef"`

// SecretKey is the key within the Kubernetes secret where the data will be injected.
SecretKey string `json:"secretKey"`

// ResponsePath is is a jq filter expression represents the path in the response where the secret value will be extracted from.
ResponsePath string `json:"responsePath"`
}

// SecretRef contains the name and namespace of a Kubernetes secret.
type SecretRef struct {
// Name is the name of the Kubernetes secret.
Name string `json:"name"`

// Namespace is the namespace of the Kubernetes secret.
Namespace string `json:"namespace"`
}

type Response struct {
StatusCode int `json:"statusCode,omitempty"`
Body string `json:"body,omitempty"`
Headers map[string][]string `json:"headers,omitempty"`
}

type Mapping struct {
Method string `json:"method"`
Body string `json:"body,omitempty"`
URL string `json:"url"`
Headers map[string][]string `json:"headers,omitempty"`
}

// A DisposableRequestStatus represents the observed state of a DisposableRequest.
type DisposableRequestStatus struct {
xpv1.ResourceStatus `json:",inline"`
Response Response `json:"response,omitempty"`
Failed int32 `json:"failed,omitempty"`
Error string `json:"error,omitempty"`
Synced bool `json:"synced,omitempty"`
RequestDetails Mapping `json:"requestDetails,omitempty"`
}

// +kubebuilder:object:root=true

// A DisposableRequest is an example API type.
// +kubebuilder:printcolumn:name="READY",type="string",JSONPath=".status.conditions[?(@.type=='Ready')].status"
// +kubebuilder:printcolumn:name="SYNCED",type="string",JSONPath=".status.conditions[?(@.type=='Synced')].status"
// +kubebuilder:printcolumn:name="EXTERNAL-NAME",type="string",JSONPath=".metadata.annotations.crossplane\\.io/external-name"
// +kubebuilder:printcolumn:name="AGE",type="date",JSONPath=".metadata.creationTimestamp"
// +kubebuilder:subresource:status
// +kubebuilder:resource:scope=Cluster,categories={crossplane,managed,http}
// +kubebuilder:storageversion
type DisposableRequest struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec DisposableRequestSpec `json:"spec"`
Status DisposableRequestStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// DisposableRequestList contains a list of DisposableRequest
type DisposableRequestList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []DisposableRequest `json:"items"`
}

// DisposableRequest type metadata.
var (
DisposableRequestKind = reflect.TypeOf(DisposableRequest{}).Name()
DisposableRequestGroupKind = schema.GroupKind{Group: Group, Kind: DisposableRequestKind}.String()
DisposableRequestKindAPIVersion = DisposableRequestKind + "." + SchemeGroupVersion.String()
DisposableRequestGroupVersionKind = SchemeGroupVersion.WithKind(DisposableRequestKind)
)

func init() {
SchemeBuilder.Register(&DisposableRequest{}, &DisposableRequestList{})
}
17 changes: 17 additions & 0 deletions apis/disposablerequest/v1alpha2/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Copyright 2022 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha2
40 changes: 40 additions & 0 deletions apis/disposablerequest/v1alpha2/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright 2020 The Crossplane Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package v1alpha2 contains the v1alpha2 group Sample resources of the http provider.
// +kubebuilder:object:generate=true
// +groupName=http.crossplane.io
// +versionName=v1alpha2
package v1alpha2

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

// Package type metadata.
const (
Group = "http.crossplane.io"
Version = "v1alpha2"
)

var (
// SchemeGroupVersion is group version used to register these objects
SchemeGroupVersion = schema.GroupVersion{Group: Group, Version: Version}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: SchemeGroupVersion}
)
34 changes: 34 additions & 0 deletions apis/disposablerequest/v1alpha2/status_setters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package v1alpha2

func (d *DisposableRequest) SetStatusCode(statusCode int) {
d.Status.Response.StatusCode = statusCode
}

func (d *DisposableRequest) SetHeaders(headers map[string][]string) {
d.Status.Response.Headers = headers
}

func (d *DisposableRequest) SetBody(body string) {
d.Status.Response.Body = body
}

func (d *DisposableRequest) SetSynced(synced bool) {
d.Status.Synced = synced
d.Status.Failed = 0
d.Status.Error = ""
}

func (d *DisposableRequest) SetError(err error) {
d.Status.Failed++
d.Status.Synced = true
if err != nil {
d.Status.Error = err.Error()
}
}

func (d *DisposableRequest) SetRequestDetails(url, method, body string, headers map[string][]string) {
d.Status.RequestDetails.Body = body
d.Status.RequestDetails.URL = url
d.Status.RequestDetails.Headers = headers
d.Status.RequestDetails.Method = method
}
Loading

0 comments on commit 76c63e4

Please sign in to comment.