Skip to content

Commit

Permalink
Merge branch 'main' into remove-dead-code
Browse files Browse the repository at this point in the history
  • Loading branch information
wdbaruni authored Oct 9, 2024
2 parents 384722c + 54e8e07 commit 9b62ffa
Show file tree
Hide file tree
Showing 17 changed files with 748 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repos:
- id: detect-aws-credentials
args: [--allow-missing-credentials]
- id: detect-private-key
exclude: testdata/.*
exclude: 'testdata/.*|test-integration/certificates/.*'
- id: check-yaml
- id: check-json
- repo: https://github.com/astral-sh/ruff-pre-commit
Expand Down
19 changes: 12 additions & 7 deletions pkg/publicapi/apimodels/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package apimodels

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -67,31 +66,37 @@ func (e *APIError) Error() string {
}

// Parse HTTP Resposne to APIError
func FromHttpResponse(resp *http.Response) (*APIError, error) {

func GenerateAPIErrorFromHTTPResponse(resp *http.Response) *APIError {
if resp == nil {
return nil, errors.New("response is nil, cannot be unmarsheld to APIError")
return NewAPIError(0, "API call error, invalid response")
}

defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %w", err)
return NewAPIError(
resp.StatusCode,
fmt.Sprintf("Unable to read API call response body. Error: %q", err.Error()))
}

var apiErr APIError
err = json.Unmarshal(body, &apiErr)
if err != nil {
return nil, fmt.Errorf("error parsing response body: %w", err)
return NewAPIError(
resp.StatusCode,
fmt.Sprintf("Unable to parse API call response body. Error: %q. Body received: %q",
err.Error(),
string(body),
))
}

// If the JSON didn't include a status code, use the HTTP Status
if apiErr.HTTPStatusCode == 0 {
apiErr.HTTPStatusCode = resp.StatusCode
}

return &apiErr, nil
return &apiErr
}

// FromBacError converts a bacerror.Error to an APIError
Expand Down
27 changes: 8 additions & 19 deletions pkg/publicapi/client/v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,12 @@ func (c *httpClient) Get(ctx context.Context, endpoint string, in apimodels.GetR
return apimodels.NewUnauthorizedError("invalid token")
}

var apiError *apimodels.APIError
if resp.StatusCode != http.StatusOK {
apiError, err = apimodels.FromHttpResponse(resp)
if err != nil {
return err
if apiError := apimodels.GenerateAPIErrorFromHTTPResponse(resp); apiError != nil {
return apiError
}
}

if apiError != nil {
return apiError
}

defer resp.Body.Close()

if out != nil {
Expand Down Expand Up @@ -116,18 +110,12 @@ func (c *httpClient) write(ctx context.Context, verb, endpoint string, in apimod
return apimodels.ErrInvalidToken
}

var apiError *apimodels.APIError
if resp.StatusCode != http.StatusOK {
apiError, err = apimodels.FromHttpResponse(resp)
if err != nil {
return err
if apiError := apimodels.GenerateAPIErrorFromHTTPResponse(resp); apiError != nil {
return apiError
}
}

if apiError != nil {
return apiError
}

if out != nil {
if err := decodeBody(resp, &out); err != nil {
return err
Expand Down Expand Up @@ -362,12 +350,13 @@ func (c *httpClient) interceptError(ctx context.Context, err error, resp *http.R
WithCode(bacerrors.UnauthorizedError)
}

apiError, apiErr := apimodels.FromHttpResponse(resp)
if apiErr == nil {
apiError := apimodels.GenerateAPIErrorFromHTTPResponse(resp)
if apiError != nil {
return apiError.ToBacError()
}

return bacerrors.Wrap(apiErr, "server error").
return bacerrors.New("server error").
WithHTTPStatusCode(http.StatusInternalServerError).
WithCode(bacerrors.InternalError)
}

Expand Down
27 changes: 27 additions & 0 deletions test-integration/Dockerfile-ClientNode
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use the docker:dind image as the base image
FROM docker:dind

# Set the working directory
WORKDIR /app

# Install curl and bash
RUN apk update && apk add --no-cache curl bash

# Install the ca-certificates package
RUN apk add --no-cache ca-certificates

# Copy a root ca into the image
COPY certificates/generated_assets/bacalhau_test_root_ca.crt /usr/local/share/ca-certificates/bacalhau_test_root_ca.crt

# Update CA certificates
RUN update-ca-certificates

# Download and execute the Bash script from the given URL
RUN curl -sSL https://get.bacalhau.org/install.sh | bash

# Download the binary, make it executable, and move it to /usr/local/bin
RUN curl -o /tmp/mc https://dl.min.io/client/mc/release/linux-amd64/mc \
&& chmod +x /tmp/mc \
&& mv /tmp/mc /usr/local/bin/

ENTRYPOINT ["dockerd-entrypoint.sh"]
24 changes: 24 additions & 0 deletions test-integration/Dockerfile-ComputeNode
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Use the docker:dind image as the base image
FROM docker:dind

# Set the working directory
WORKDIR /app

# Install curl and bash
RUN apk update && apk add --no-cache curl bash

# Install the ca-certificates package
RUN apk add --no-cache ca-certificates

# Copy a root ca into the image
COPY certificates/generated_assets/bacalhau_test_root_ca.crt /usr/local/share/ca-certificates/bacalhau_test_root_ca.crt

# Update CA certificates
RUN update-ca-certificates

# Download and execute the Bash script from the given URL
RUN curl -sSL https://get.bacalhau.org/install.sh | bash

COPY compute_node_image_setup.sh compute_node_image_setup.sh
ENTRYPOINT ["/usr/bin/env"]
CMD ./compute_node_image_setup.sh
24 changes: 24 additions & 0 deletions test-integration/Dockerfile-DockerImageRegistryNode
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM registry:2

# Install curl and bash
RUN apk update && apk add --no-cache curl bash

# Install the ca-certificates package
RUN apk add --no-cache ca-certificates

# Copy a root ca into the image
COPY certificates/generated_assets/bacalhau_test_root_ca.crt /usr/local/share/ca-certificates/bacalhau_test_root_ca.crt

# Create a directory to store certificates to be used by the registry
RUN mkdir /certs

# Copy the certificate and key from the local directory to /certs
COPY certificates/generated_assets/bacalhau-container-img-registry-node.crt /certs/
COPY certificates/generated_assets/bacalhau-container-img-registry-node.key /certs/

# Ensure proper permissions for certs
RUN chmod 600 /certs/bacalhau-container-img-registry-node.key
RUN chmod 644 /certs/bacalhau-container-img-registry-node.crt

# Expose the registry's default port
EXPOSE 5000 443
22 changes: 22 additions & 0 deletions test-integration/Dockerfile-RequesterNode
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Use the docker:dind image as the base image
FROM docker:dind

# Set the working directory
WORKDIR /app

# Install curl and bash
RUN apk update && apk add --no-cache curl bash

# Install the ca-certificates package
RUN apk add --no-cache ca-certificates

# Copy a root ca into the image
COPY certificates/generated_assets/bacalhau_test_root_ca.crt /usr/local/share/ca-certificates/bacalhau_test_root_ca.crt

# Update CA certificates
RUN update-ca-certificates

# Download and execute the Bash script from the given URL
RUN curl -sSL https://get.bacalhau.org/install.sh | bash

ENTRYPOINT ["dockerd-entrypoint.sh"]
Loading

0 comments on commit 9b62ffa

Please sign in to comment.