-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
255 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2015 Google Inc. All Rights Reserved. | ||
// | ||
// 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 main | ||
|
||
import ( | ||
"log" | ||
distreference "github.com/docker/distribution/reference" | ||
) | ||
|
||
// Specification for image referecne validation request. | ||
type ImageReferenceValiditySpec struct { | ||
// Reference of the image | ||
Reference string `json:"reference"` | ||
} | ||
|
||
// Describe validity of the image reference. | ||
type ImageReferenceValidity struct { | ||
// True when the image reference is valid. | ||
Valid bool `json:"valid"` | ||
// Error reason when image reference is valid | ||
Reason string `json:"reason"` | ||
} | ||
|
||
// Validation image reference. | ||
func ValidateImageReference(spec *ImageReferenceValiditySpec) (*ImageReferenceValidity, error) { | ||
log.Printf("Validating %s as a image reference", spec.Reference) | ||
|
||
s := spec.Reference | ||
_, err := distreference.ParseNamed(s) | ||
if err != nil { | ||
return &ImageReferenceValidity{Valid: false, Reason: err.Error()}, nil | ||
} | ||
return &ImageReferenceValidity{Valid: true}, nil | ||
} |
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,67 @@ | ||
// Copyright 2015 Google Inc. All Rights Reserved. | ||
// | ||
// 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. | ||
|
||
/** The name of this directive. */ | ||
export const validImageReferenceValidationKey = 'validImageReference'; | ||
|
||
/** | ||
* Validates image reference | ||
* | ||
* @param {!angular.$resource} $resource | ||
* @param {!angular.$q} $q | ||
* @return {!angular.Directive} | ||
* @ngInject | ||
*/ | ||
export default function validImageReferenceDirective($resource, $q) { | ||
return { | ||
restrict: 'A', | ||
require: 'ngModel', | ||
link: function(scope, element, attrs, ctrl) { | ||
/** @type {!angular.NgModelController} */ | ||
let ngModelController = ctrl; | ||
|
||
ngModelController.$asyncValidators[validImageReferenceValidationKey] = | ||
(reference) => { return validate(reference, $resource, $q); }; | ||
}, | ||
}; | ||
} | ||
|
||
/** | ||
* @param {string} reference | ||
* @param {!angular.$resource} resource | ||
* @param {!angular.$q} q | ||
*/ | ||
function validate(reference, resource, q) { | ||
let deferred = q.defer(); | ||
|
||
/** @type {!angular.Resource<!backendApi.ProtocolValiditySpec>} */ | ||
let resourceClass = resource('api/v1/appdeployments/validate/imagereference'); | ||
/** @type {!backendApi.ImageReferenceValiditySpec} */ | ||
let spec = {reference: reference}; | ||
resourceClass.save( | ||
spec, | ||
/** | ||
* @param {!backendApi.ImageReferenceValidity} validity | ||
*/ | ||
(validity) => { | ||
if (validity.valid === true) { | ||
deferred.resolve(); | ||
} else { | ||
deferred.reject(validity.reason); | ||
} | ||
}, | ||
() => { deferred.reject(); }); | ||
|
||
return deferred.promise; | ||
} |
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,102 @@ | ||
// Copyright 2015 Google Inc. All Rights Reserved. | ||
// | ||
// 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 main | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestValidateImageReference(t *testing.T) { | ||
cases := []struct { | ||
reference string | ||
expected bool | ||
}{ | ||
{ | ||
"test", | ||
true, | ||
}, | ||
{ | ||
"test:1", | ||
true, | ||
}, | ||
{ | ||
"test:tag", | ||
true, | ||
}, | ||
{ | ||
"private.registry:5000/test:1", | ||
true, | ||
}, | ||
{ | ||
"private.registry:5000/test", | ||
true, | ||
}, | ||
{ | ||
"private.registry:5000/namespace/test:1", | ||
true, | ||
}, | ||
{ | ||
"private.registry:port/namespace/test:1", | ||
false, | ||
}, | ||
{ | ||
"private.registry:5000/n/a/m/e/s/test:1", | ||
true, | ||
}, | ||
{ | ||
"private.registry:5000/namespace/test:image:1", | ||
false, | ||
}, | ||
{ | ||
":", | ||
false, | ||
}, | ||
{ | ||
"private.registry:5000/test:1@sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", | ||
true, | ||
}, | ||
{ | ||
"Test", | ||
false, | ||
}, | ||
{ | ||
"private.registry:5000/Test", | ||
false, | ||
}, | ||
{ | ||
"private@registry:5000/test", | ||
false, | ||
}, | ||
{ | ||
"", | ||
false, | ||
}, | ||
{ | ||
"test image:1", | ||
false, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
spec := &ImageReferenceValiditySpec{ | ||
Reference: c.reference, | ||
} | ||
validity, _ := ValidateImageReference(spec) | ||
if validity.Valid != c.expected { | ||
t.Errorf("Expected %#v validity to be %#v, but was %#v\n", | ||
c.reference, c.expected, validity) | ||
} | ||
} | ||
} |