Skip to content

Commit

Permalink
Merge pull request #88 from projectsyn/gitlab.com_detection
Browse files Browse the repository at this point in the history
Fix gitlab.com detection due to CloudFlare
  • Loading branch information
Simon Rüegg authored Jul 23, 2020
2 parents ea14a51 + 3765bcf commit b45e063
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 54 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Documentation structure
### Fixed
- Vault nilpointer if `SKIP_VAULT_SETUP` is set
- Fix broken `gitlab.com` detection due to CloudFlare checking

## v0.1.5 - 2020-06-12
### Added
Expand Down
27 changes: 3 additions & 24 deletions pkg/git/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package gitlab

import (
"fmt"
"net/http"
"net/url"
"strings"
"time"

"github.com/projectsyn/lieutenant-operator/pkg/git/helpers"
"github.com/projectsyn/lieutenant-operator/pkg/git/manager"
Expand Down Expand Up @@ -225,30 +223,11 @@ func (g *Gitlab) FullURL() *url.URL {
return sshURL
}

// IsType determines if the given url can be handled by this concrete implementation.
// This is done by a simple http query to the login page of gitlab. If any errors occur anywhere
// it will return false.
// TODO: this will be deprecated in favour of a fixed type definition in the
// CRD. As there's currently only the GitLab implementation this is a workaround
// for the brittle detection.
func (g *Gitlab) IsType(URL *url.URL) (bool, error) {

httpClient := &http.Client{
Timeout: 10 * time.Second,
}

gitlabURL := URL.Scheme + "://" + URL.Host + "/users/sign_in'"

resp, err := httpClient.Get(gitlabURL)
if err != nil {
return false, err
}

if resp.StatusCode == http.StatusNotFound {
return false, nil
}

if resp.StatusCode != http.StatusOK {
return false, fmt.Errorf("status code was %d", resp.StatusCode)
}

return true, nil
}

Expand Down
30 changes: 0 additions & 30 deletions pkg/git/gitlab/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,36 +73,6 @@ func TestGitlab_Read(t *testing.T) {
}
}

func TestGitlab_IsType(t *testing.T) {
tests := []struct {
name string
want bool
httpServer *httptest.Server
}{
{
name: "is gitlab",
want: true,
httpServer: testGetHTTPServer(http.StatusOK, []byte("")),
},
{
name: "is not Gitlab",
want: false,
httpServer: testGetHTTPServer(http.StatusNotFound, []byte("")),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer tt.httpServer.Close()
g := &Gitlab{}
serverURL, _ := url.Parse(tt.httpServer.URL)
if got, _ := g.IsType(serverURL); got != tt.want {
t.Errorf("IsType() = %v, want %v", got, tt.want)
}
})
}

}

func testGetCreateServer() *httptest.Server {
mux := http.NewServeMux()

Expand Down

0 comments on commit b45e063

Please sign in to comment.