forked from goharbor/harbor
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to basic authentication for registry
1. Add basic authorizer for registry which modify the request to add basic authorization header to request based on configuration. 2. Set basic auth header for proxy when accessing registry 3. Switche the registry to use basic auth by default and use the basic authorizer to access Harbor. Signed-off-by: Daniel Jiang <jiangd@vmware.com>
- Loading branch information
1 parent
a1b25e1
commit 4026b7a
Showing
16 changed files
with
173 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright Project Harbor 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 auth | ||
|
||
import ( | ||
"github.com/goharbor/harbor/src/common/http/modifier" | ||
"github.com/goharbor/harbor/src/core/config" | ||
"sync" | ||
) | ||
|
||
// NewBasicAuthorizer create an authorizer to add basic auth header as is set in the parameter | ||
func NewBasicAuthorizer(u, p string) modifier.Modifier { | ||
return NewBasicAuthCredential(u, p) | ||
} | ||
|
||
var ( | ||
defaultAuthorizer modifier.Modifier | ||
once sync.Once | ||
) | ||
|
||
// DefaultBasicAuthorizer returns the basic authorizer that sets the basic auth as configured in env variables | ||
func DefaultBasicAuthorizer() modifier.Modifier { | ||
once.Do(func() { | ||
u, p := config.RegistryCredential() | ||
defaultAuthorizer = NewBasicAuthCredential(u, p) | ||
}) | ||
return defaultAuthorizer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright Project Harbor 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 auth | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestDefaultBasicAuthorizer(t *testing.T) { | ||
os.Setenv("REGISTRY_CREDENTIAL_USERNAME", "testuser") | ||
os.Setenv("REGISTRY_CREDENTIAL_PASSWORD", "testpassword") | ||
defer func() { | ||
os.Unsetenv("REGISTRY_CREDENTIAL_USERNAME") | ||
os.Unsetenv("REGISTRY_CREDENTIAL_PASSWORD") | ||
}() | ||
req, _ := http.NewRequest(http.MethodGet, "http://127.0.0.1", nil) | ||
a := DefaultBasicAuthorizer() | ||
err := a.Modify(req) | ||
assert.Nil(t, err) | ||
u, p, ok := req.BasicAuth() | ||
assert.True(t, ok) | ||
assert.Equal(t, "testuser", u) | ||
assert.Equal(t, "testpassword", p) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.