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

Added /gitproxy API endpoint #2550

Merged
merged 6 commits into from
Oct 2, 2023
Merged
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
64 changes: 64 additions & 0 deletions acceptance/api/v1/gitproxy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright © 2021 - 2023 SUSE LLC
// 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 v1_test

import (
"encoding/json"
"net/http"
"strings"

"github.com/epinio/epinio/pkg/api/core/v1/models"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Gitproxy endpoint", LMisc, func() {
It("proxies the request to github", func() {
gitproxyRequest := models.GitProxyRequest{
URL: "https://api.github.com/repos/epinio/epinio",
}

resp, statusCode := gitproxy(toJSON(gitproxyRequest))
Expect(statusCode).To(Equal(http.StatusOK))

var m map[string]interface{}
err := json.Unmarshal(resp, &m)
Expect(err).ToNot(HaveOccurred())

Expect(m["id"]).To(BeEquivalentTo(311485110))
Expect(m["full_name"]).To(Equal("epinio/epinio"))
})

It("fails for unknown endpoint", func() {
gitproxyRequest := models.GitProxyRequest{
URL: "https://example.com",
}

resp, statusCode := gitproxy(toJSON(gitproxyRequest))
ExpectBadRequestError(resp, statusCode, "invalid proxied URL: unknown URL 'https://example.com'")
})

It("fails for invalid JSON", func() {
resp, statusCode := gitproxy(strings.NewReader(`sjkskl`))
ExpectBadRequestError(resp, statusCode, "invalid character 's' looking for beginning of value")
})

It("fails for non whitelisted APIs", func() {
gitproxyRequest := models.GitProxyRequest{
URL: "https://api.github.com/users",
}

resp, statusCode := gitproxy(toJSON(gitproxyRequest))
ExpectBadRequestError(resp, statusCode, "invalid proxied URL: invalid Github URL: '/users'")
})
})
7 changes: 7 additions & 0 deletions acceptance/api/v1/helpers_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ func appImportGit(namespace, app, gitURL, revision string) ([]byte, int) {
return bodyBytes, response.StatusCode
}

func gitproxy(body io.Reader) ([]byte, int) {
GinkgoHelper()

endpoint := makeEndpoint(v1.Routes.Path("GitProxy"))
return curl(http.MethodPost, endpoint, body)
}

func curl(method, endpoint string, body io.Reader) ([]byte, int) {
GinkgoHelper()

Expand Down
23 changes: 23 additions & 0 deletions helpers/cahash/cahash.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,26 @@

return nil, errors.New("failed find PEM data")
}

// DecodeCerts iterates over pem blocks and load all the valid certificates
func DecodeCerts(raw []byte) ([]*x509.Certificate, error) {
certs := []*x509.Certificate{}

byteData := raw
for len(byteData) > 0 {
block, rest := pem.Decode(byteData)
if block == nil {
return nil, errors.New("failed decoding PEM data")
}

Check warning on line 73 in helpers/cahash/cahash.go

View check run for this annotation

Codecov / codecov/patch

helpers/cahash/cahash.go#L65-L73

Added lines #L65 - L73 were not covered by tests

cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
byteData = rest
continue // pem block is not a cert? (e.g. maybe it was a dh_params block)

Check warning on line 78 in helpers/cahash/cahash.go

View check run for this annotation

Codecov / codecov/patch

helpers/cahash/cahash.go#L75-L78

Added lines #L75 - L78 were not covered by tests
}

certs = append(certs, cert)

Check warning on line 81 in helpers/cahash/cahash.go

View check run for this annotation

Codecov / codecov/patch

helpers/cahash/cahash.go#L81

Added line #L81 was not covered by tests
}

return certs, nil

Check warning on line 84 in helpers/cahash/cahash.go

View check run for this annotation

Codecov / codecov/patch

helpers/cahash/cahash.go#L84

Added line #L84 was not covered by tests
}
4 changes: 2 additions & 2 deletions internal/api/v1/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/epinio/epinio/internal/auth"
"github.com/epinio/epinio/internal/cli/server/requestctx"
"github.com/gin-gonic/gin"
"github.com/go-logr/stdr"
"github.com/go-logr/logr"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
Expand All @@ -35,7 +35,7 @@ var _ = Describe("Authorization Middleware", func() {
gin.SetMode(gin.TestMode)
w = httptest.NewRecorder()
c, _ = gin.CreateTestContext(w)
ctx = requestctx.WithLogger(context.Background(), stdr.New(nil))
ctx = requestctx.WithLogger(context.Background(), logr.Discard())
url = "http://url.com/endpoint"
})

Expand Down
Loading
Loading