Skip to content

Commit

Permalink
feat: enable/disable ssl cert verification in test trigger (#2521)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgeepc committed May 26, 2023
1 parent 05dc8dc commit 0bcfa3a
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 31 deletions.
3 changes: 3 additions & 0 deletions api/http.yaml
Expand Up @@ -25,6 +25,9 @@ components:
type: string
auth:
$ref: "#/components/schemas/HTTPAuth"
sslVerification:
type: boolean
default: false
# proxy:
# type: object
# description: TODO
Expand Down
50 changes: 45 additions & 5 deletions cli/openapi/model_http_request.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions server/executor/trigger/http.go
Expand Up @@ -3,6 +3,7 @@ package trigger
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io"
"net"
Expand All @@ -24,11 +25,12 @@ func HTTP() Triggerer {

type httpTriggerer struct{}

func httpClient() http.Client {
func httpClient(sslVerification bool) http.Client {
transport := &http.Transport{
DialContext: (&net.Dialer{
Timeout: time.Second,
}).DialContext,
TLSClientConfig: &tls.Config{InsecureSkipVerify: !sslVerification},
}

return http.Client{
Expand Down Expand Up @@ -71,7 +73,7 @@ func (te *httpTriggerer) Trigger(ctx context.Context, test model.Test, opts *Tri
return response, fmt.Errorf(`trigger type "%s" not supported by HTTP triggerer`, trigger.Type)
}

client := httpClient()
client := httpClient(trigger.HTTP.SSLVerification)

ctx = trace.ContextWithSpanContext(ctx, newSpanContext(ctx))
ctx, cncl := context.WithTimeout(ctx, 30*time.Second)
Expand Down
22 changes: 12 additions & 10 deletions server/http/mappings/http.go
Expand Up @@ -22,11 +22,12 @@ func (m OpenAPI) HTTPRequest(in *model.HTTPRequest) openapi.HttpRequest {
}

return openapi.HttpRequest{
Url: in.URL,
Method: string(in.Method),
Headers: m.HTTPHeaders(in.Headers),
Body: in.Body,
Auth: m.Auth(in.Auth),
Url: in.URL,
Method: string(in.Method),
Headers: m.HTTPHeaders(in.Headers),
Body: in.Body,
Auth: m.Auth(in.Auth),
SslVerification: in.SSLVerification,
}
}

Expand Down Expand Up @@ -89,11 +90,12 @@ func (m Model) HTTPRequest(in openapi.HttpRequest) *model.HTTPRequest {
}

return &model.HTTPRequest{
URL: in.Url,
Method: model.HTTPMethod(in.Method),
Headers: m.HTTPHeaders(in.Headers),
Body: in.Body,
Auth: m.Auth(in.Auth),
URL: in.Url,
Method: model.HTTPMethod(in.Method),
Headers: m.HTTPHeaders(in.Headers),
Body: in.Body,
Auth: m.Auth(in.Auth),
SSLVerification: in.SslVerification,
}
}

Expand Down
11 changes: 6 additions & 5 deletions server/model/http.go
Expand Up @@ -32,11 +32,12 @@ type HTTPHeader struct {
}

type HTTPRequest struct {
Method HTTPMethod `expr_enabled:"true" json:"method,omitempty"`
URL string `expr_enabled:"true" json:"url"`
Body string `expr_enabled:"true" json:"body,omitempty"`
Headers []HTTPHeader `json:"headers,omitempty"`
Auth *HTTPAuthenticator `json:"auth,omitempty"`
Method HTTPMethod `expr_enabled:"true" json:"method,omitempty"`
URL string `expr_enabled:"true" json:"url"`
Body string `expr_enabled:"true" json:"body,omitempty"`
Headers []HTTPHeader `json:"headers,omitempty"`
Auth *HTTPAuthenticator `json:"auth,omitempty"`
SSLVerification bool `json:"sslVerification,omitempty"`
}

func (a HTTPRequest) Authenticate(req *http.Request) {
Expand Down
11 changes: 6 additions & 5 deletions server/model/yaml/http.go
Expand Up @@ -5,11 +5,12 @@ import (
)

type HTTPRequest struct {
URL string `yaml:"url"`
Method string `yaml:"method"`
Headers []HTTPHeader `yaml:"headers,omitempty" dc:"headers"`
Authentication *HTTPAuthentication `yaml:"authentication,omitempty" dc:"auth"`
Body string `yaml:"body,omitempty"`
URL string `yaml:"url"`
Method string `yaml:"method"`
Headers []HTTPHeader `yaml:"headers,omitempty" dc:"headers"`
Authentication *HTTPAuthentication `yaml:"authentication,omitempty" dc:"auth"`
Body string `yaml:"body,omitempty"`
SSLVerification bool `yaml:"sslVerification,omitempty"`
}

func (r HTTPRequest) Validate() error {
Expand Down
2 changes: 2 additions & 0 deletions server/openapi/model_http_request.go
Expand Up @@ -19,6 +19,8 @@ type HttpRequest struct {
Body string `json:"body,omitempty"`

Auth HttpAuth `json:"auth,omitempty"`

SslVerification bool `json:"sslVerification,omitempty"`
}

// AssertHttpRequestRequired checks if the required fields are not zero-ed
Expand Down
Expand Up @@ -32,3 +32,9 @@ export const DeleteIcon = styled(DeleteOutlined)`
color: ${({theme}) => theme.color.textSecondary};
font-size: ${({theme}) => theme.size.md};
`;

export const SSLVerificationContainer = styled.div`
align-items: center;
display: flex;
gap: 8px;
`;
Expand Up @@ -5,6 +5,7 @@ import {BodyField} from './BodyField/BodyField';
import RequestDetailsAuthInput from './RequestDetailsAuthInput/RequestDetailsAuthInput';
import RequestDetailsHeadersInput from './RequestDetailsHeadersInput';
import RequestDetailsUrlInput from './RequestDetailsUrlInput';
import SSLVerification from './SSLVerification';

export const FORM_ID = 'create-test';

Expand All @@ -19,6 +20,7 @@ const RequestDetailsForm = ({form}: IProps) => {
<RequestDetailsAuthInput />
<RequestDetailsHeadersInput />
<BodyField setBody={body => form.setFieldsValue({body})} body={Form.useWatch('body', form)} />
<SSLVerification />
</S.InputContainer>
);
};
Expand Down
@@ -0,0 +1,17 @@
import {Form, Switch} from 'antd';
import {TooltipQuestion} from 'components/TooltipQuestion/TooltipQuestion';
import * as S from './RequestDetails.styled';

const SSLVerification = () => {
return (
<S.SSLVerificationContainer>
<label htmlFor="sslVerification">Enable SSL certificate verification</label>
<Form.Item name="sslVerification" valuePropName="checked">
<Switch />
</Form.Item>
<TooltipQuestion title="Verify SSL certificates when sending the request. Verification failures will result in the request being aborted." />
</S.SSLVerificationContainer>
);
};

export default SSLVerification;
10 changes: 9 additions & 1 deletion web/src/models/HttpRequest.model.ts
Expand Up @@ -9,7 +9,14 @@ type HttpRequest = Model<
}
>;

const HttpRequest = ({method = 'GET', url = '', headers = [], body = '', auth = {}}: TRawHTTPRequest): HttpRequest => {
const HttpRequest = ({
method = 'GET',
url = '',
headers = [],
body = '',
auth = {},
sslVerification = false,
}: TRawHTTPRequest): HttpRequest => {
return {
method,
url,
Expand All @@ -19,6 +26,7 @@ const HttpRequest = ({method = 'GET', url = '', headers = [], body = '', auth =
})),
body,
auth,
sslVerification,
};
};

Expand Down
1 change: 1 addition & 0 deletions web/src/services/Triggers/Curl.service.ts
Expand Up @@ -37,6 +37,7 @@ const CurlTriggerService = (): ICurlTriggerService => ({
method: method as HTTP_METHOD,
headers: Object.entries(headers).map(([key, value]) => ({key, value})),
body: body === 'data' ? '' : JSON.stringify(body),
sslVerification: false,
};
},

Expand Down
7 changes: 4 additions & 3 deletions web/src/services/Triggers/Http.service.ts
Expand Up @@ -5,9 +5,9 @@ import HttpRequest from 'models/HttpRequest.model';

const HttpTriggerService = (): ITriggerService => ({
async getRequest(values) {
const {url, method, auth, headers, body} = values as IHttpValues;
const {url, method, auth, headers, body, sslVerification} = values as IHttpValues;

return HttpRequest({url, method, auth, headers, body});
return HttpRequest({url, method, auth, headers, body, sslVerification});
},

async validateDraft(draft): Promise<boolean> {
Expand All @@ -16,14 +16,15 @@ const HttpTriggerService = (): ITriggerService => ({
},

getInitialValues(request) {
const {url, method, headers, body, auth} = request as HttpRequest;
const {url, method, headers, body, auth, sslVerification} = request as HttpRequest;

return {
url,
auth,
method: method as HTTP_METHOD,
headers,
body,
sslVerification,
};
},
});
Expand Down
1 change: 1 addition & 0 deletions web/src/types/Generated.types.ts
Expand Up @@ -1486,6 +1486,7 @@ export interface external {
headers?: external["http.yaml"]["components"]["schemas"]["HTTPHeader"][];
body?: string;
auth?: external["http.yaml"]["components"]["schemas"]["HTTPAuth"];
sslVerification?: boolean;
};
HTTPResponse: {
status?: string;
Expand Down
1 change: 1 addition & 0 deletions web/src/types/Test.types.ts
Expand Up @@ -34,6 +34,7 @@ export interface IHttpValues {
headers: HttpRequest['headers'];
method: HTTP_METHOD;
url: string;
sslVerification: boolean;
}

export interface RequestDefinitionExtended extends Request {
Expand Down

0 comments on commit 0bcfa3a

Please sign in to comment.