Skip to content

Commit

Permalink
nosearch: fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasmatus committed Aug 20, 2024
1 parent c0e97ac commit 5a5fe4a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
26 changes: 12 additions & 14 deletions src/ImageRunModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -342,21 +342,22 @@ export class ImageRunModal extends React.Component {
});
};

buildFilterRegex(searchText) {
buildFilterRegex(searchText, includeRegistry) {
// Strip out all non-allowed container image characters when filtering.
let regexString = searchText.replace(/[^/\w_.:-]/g, "");
// Strip image registry option if set for comparing results for docker.io searching for docker.io/fedora
// returns docker.io/$username/fedora for example.
if (regexString.includes('/')) {
regexString = searchText.replace(searchText.split('/')[0], '');
// drop registry from regex to allow filtering only by container names
if (!includeRegistry && regexString.includes('/')) {
regexString = '/' + searchText.split('/')
.slice(1)
.join('/');
}

return new RegExp(regexString, 'i');
}

onSearchTriggered = value => {
const imageExistsLocally = (searchText, localImages) => {
const regex = this.buildFilterRegex(searchText);
const regex = this.buildFilterRegex(searchText, true);
return localImages.some(localImage => localImage.Name.search(regex) !== -1);
};

Expand Down Expand Up @@ -402,7 +403,8 @@ export class ImageRunModal extends React.Component {
}

// Query if used url is a valid container that can be pulled
if ((value.includes('.') || value.startsWith("localhost")) && value.includes('/')) {
// TODO
if (value.includes('/')) {
searches.push(this.activeConnection.call({
method: "GET",
path: client.VERSION + "libpod/manifests/" + value + "/json",
Expand All @@ -419,18 +421,14 @@ export class ImageRunModal extends React.Component {

for (const result of reply) {
if (result.status === "fulfilled") {
try {
imageResults = imageResults.concat(JSON.parse(result.value));
} catch (err) {
console.error(err);
}
imageResults = imageResults.concat(JSON.parse(result.value));
} else {
// manifests query returns 404 Not Found in case used URL is not an image
// supress this error as it will either result in "No images found" or prioritize other erros from search queries
if (result.reason.status === 404) {
continue;
}
// Supress "could't search registry" error when local image is found
// Supress "couldn't search registry" error when local image is found
if (result.reason.message.includes("couldn't search registry") && imageExistsLocally(value, this.props.localImages)) {
continue;
}
Expand Down Expand Up @@ -570,7 +568,7 @@ export class ImageRunModal extends React.Component {
imageRegistries.push(this.state.searchByRegistry);
}

const input = this.buildFilterRegex(searchText);
const input = this.buildFilterRegex(searchText, false);

const results = imageRegistries
.map((reg, index) => {
Expand Down
16 changes: 7 additions & 9 deletions test/check-application
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ IMG_BUSYBOX_LATEST = IMG_BUSYBOX + ":latest"
IMG_REGISTRY = "localhost/test-registry"
IMG_REGISTRY_LATEST = IMG_REGISTRY + ":latest"

# nginx configuration to simulate a registry without search API (like ghcr.io)
NGINX_DEFAULT_CONF = """
server {
listen 80;
listen [::]:80;
listen [::]:80;
server_name localhost;
location / {
proxy_pass http://localhost:5000/;
}
# Simulate a registry without search API, like ghcr.io
location ^~ /v2/_catalog {
return 403;
}
Expand Down Expand Up @@ -3030,16 +3032,12 @@ class TestApplication(testlib.MachineCase):
self.setupRegistry()

# Nginx config simulates behavior of ghcr.io in terms of `podman search` and `podman manifest inspect`
self.execute(True, f"""
echo '{NGINX_DEFAULT_CONF}' > /etc/nginx/conf.d/default.conf
self.write_file("/etc/nginx/conf.d/default.conf", NGINX_DEFAULT_CONF,
post_restore_action="systemctl stop nginx.service; setsebool -P httpd_can_network_connect 0")
self.execute(True, """
setsebool -P httpd_can_network_connect 1
systemctl start nginx.service
""")
self.addCleanup(m.execute, """
systemctl stop nginx.service
setsebool -P httpd_can_network_connect 0
rm -f /etc/nginx/default.conf
""")

container_name = "localhost:80/my-busybox"
m.execute(f"podman tag {IMG_BUSYBOX} {container_name}; podman push {container_name}")
Expand Down Expand Up @@ -3072,9 +3070,9 @@ class TestApplication(testlib.MachineCase):
# "couldn't search registry" error is hidden when some local image is found
b.set_input_text("#create-image-image-select-typeahead", "")
b.set_input_text("#create-image-image-select-typeahead", "localhost:80/my-busy")
b.wait_text("button.pf-v5-c-select__menu-item:not(.pf-m-disabled)", f"{container_name}:latest")
b.wait_not_present(".pf-v5-c-alert.pf-m-danger")
b.click('button.pf-v5-c-toggle-group__button:contains("Local")')
b.wait_text("button.pf-v5-c-select__menu-item:not(.pf-m-disabled)", f"{container_name}:latest")

# Error is shown again when no image was found locally
b.set_input_text("#create-image-image-select-typeahead", "localhost:80/their-busy")
Expand Down

0 comments on commit 5a5fe4a

Please sign in to comment.