Skip to content

Commit f7150e8

Browse files
committed
Use official checksums to verify Tini (#55717)
Firstly, backport the use of tini as the Docker entrypoint. This was supposed to have been done following #50277, but was missed. It isn't a direct backport as this branch will continue using root as the initial Docker user. Secondly, backport #55491 to use the official checksums when downloading tini.
1 parent 9ba83ef commit f7150e8

File tree

3 files changed

+66
-23
lines changed

3 files changed

+66
-23
lines changed

distribution/docker/build.gradle

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,32 @@ dependencies {
2828
}
2929

3030
ext.expansions = { architecture, oss, local ->
31+
String base_image = null
32+
String tini_arch = null
33+
String classifier = null
3134
switch (architecture) {
3235
case "aarch64":
36+
base_image = "arm64v8/centos:7"
37+
tini_arch = "arm64"
38+
classifier = "linux-aarch64"
39+
break;
3340
case "x64":
41+
base_image = "amd64/centos:7"
42+
tini_arch = "amd64"
43+
classifier = "linux-x86_64"
3444
break;
3545
default:
3646
throw new IllegalArgumentException("unrecongized architecture [" + architecture + "], must be one of (aarch64|x64)")
3747
}
38-
final String classifier = "aarch64".equals(architecture) ? "linux-aarch64" : "linux-x86_64"
3948
final String elasticsearch = oss ? "elasticsearch-oss-${VersionProperties.elasticsearch}-${classifier}.tar.gz" : "elasticsearch-${VersionProperties.elasticsearch}-${classifier}.tar.gz"
4049
return [
41-
'base_image' : "aarch64".equals(architecture) ? "arm64v8/centos:7" : "centos:7",
50+
'base_image' : base_image,
4251
'build_date' : BuildParams.buildDate,
4352
'elasticsearch' : elasticsearch,
4453
'git_revision' : BuildParams.gitRevision,
4554
'license' : oss ? 'Apache-2.0' : 'Elastic-License',
4655
'source_elasticsearch': local ? "COPY $elasticsearch /opt/" : "RUN cd /opt && curl --retry 8 -s -L -O https://artifacts.elastic.co/downloads/elasticsearch/${elasticsearch} && cd -",
56+
'tini_arch' : tini_arch,
4757
'version' : VersionProperties.elasticsearch
4858
]
4959
}
@@ -227,6 +237,7 @@ subprojects { Project subProject ->
227237
def tarFile = "${parent.projectDir}/build/elasticsearch${"aarch64".equals(architecture) ? '-aarch64' : ''}${oss ? '-oss' : ''}_test.${VersionProperties.elasticsearch}.docker.tar"
228238

229239
final Task exportDockerImageTask = task(exportTaskName, type: LoggedExec) {
240+
inputs.file("${parent.projectDir}/build/markers/${buildTaskName}.marker")
230241
executable 'docker'
231242
outputs.file(tarFile)
232243
args "save",

distribution/docker/src/docker/Dockerfile

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,22 @@
1414
FROM ${base_image} AS builder
1515

1616
RUN for iter in {1..10}; do yum update --setopt=tsflags=nodocs -y && \
17-
yum install --setopt=tsflags=nodocs -y gzip shadow-utils tar && \
17+
yum install --setopt=tsflags=nodocs -y wget gzip shadow-utils tar && \
1818
yum clean all && exit_code=0 && break || exit_code=\$? && echo "yum error: retry \$iter in 10s" && sleep 10; done; \
1919
(exit \$exit_code)
2020

21+
# `tini` is a tiny but valid init for containers. This is used to cleanly
22+
# control how ES and any child processes are shut down.
23+
#
24+
# The tini GitHub page gives instructions for verifying the binary using
25+
# gpg, but the keyservers are slow to return the key and this can fail the
26+
# build. Instead, we check the binary against a checksum that they provide.
27+
RUN wget --no-cookies --quiet https://github.com/krallin/tini/releases/download/v0.19.0/tini-${tini_arch} \
28+
&& wget --no-cookies --quiet https://github.com/krallin/tini/releases/download/v0.19.0/tini-${tini_arch}.sha256sum \
29+
&& sha256sum -c tini-${tini_arch}.sha256sum \
30+
&& mv tini-${tini_arch} /tini \
31+
&& chmod +x /tini
32+
2133
ENV PATH /usr/share/elasticsearch/bin:\$PATH
2234

2335
RUN groupadd -g 1000 elasticsearch && \
@@ -45,6 +57,8 @@ FROM ${base_image}
4557

4658
ENV ELASTIC_CONTAINER true
4759

60+
COPY --from=builder /tini /tini
61+
4862
RUN for iter in {1..10}; do yum update --setopt=tsflags=nodocs -y && \
4963
yum install --setopt=tsflags=nodocs -y nc shadow-utils zip unzip && \
5064
yum clean all && exit_code=0 && break || exit_code=\$? && echo "yum error: retry \$iter in 10s" && sleep 10; done; \
@@ -65,17 +79,14 @@ RUN ln -sf /etc/pki/ca-trust/extracted/java/cacerts /usr/share/elasticsearch/jdk
6579

6680
ENV PATH /usr/share/elasticsearch/bin:\$PATH
6781

68-
COPY --chown=1000:0 bin/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
82+
COPY bin/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
83+
84+
RUN chmod g=u /etc/passwd && \
85+
chmod 0775 /usr/local/bin/docker-entrypoint.sh
6986

7087
# Ensure that there are no files with setuid or setgid, in order to mitigate "stackclash" attacks.
7188
RUN find / -xdev -perm -4000 -exec chmod ug-s {} +
7289

73-
# Openshift overrides USER and uses ones with randomly uid>1024 and gid=0
74-
# Allow ENTRYPOINT (and ES) to run even with a different user
75-
RUN chgrp 0 /usr/local/bin/docker-entrypoint.sh && \
76-
chmod g=u /etc/passwd && \
77-
chmod 0775 /usr/local/bin/docker-entrypoint.sh
78-
7990
EXPOSE 9200 9300
8091

8192
LABEL org.label-schema.build-date="${build_date}" \
@@ -98,7 +109,7 @@ LABEL org.label-schema.build-date="${build_date}" \
98109
org.opencontainers.image.vendor="Elastic" \
99110
org.opencontainers.image.version="${version}"
100111

101-
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
112+
ENTRYPOINT ["/tini", "--", "/usr/local/bin/docker-entrypoint.sh"]
102113
# Dummy overridable parameter parsed by entrypoint
103114
CMD ["eswrapper"]
104115

qa/os/src/test/java/org/elasticsearch/packaging/test/DockerTests.java

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import static org.hamcrest.Matchers.is;
6969
import static org.hamcrest.Matchers.not;
7070
import static org.hamcrest.Matchers.nullValue;
71+
import static org.hamcrest.Matchers.startsWith;
7172
import static org.junit.Assume.assumeFalse;
7273
import static org.junit.Assume.assumeTrue;
7374

@@ -139,7 +140,7 @@ public void test040JavaUsesTheOsProvidedKeystore() {
139140
/**
140141
* Checks that there are Amazon trusted certificates in the cacaerts keystore.
141142
*/
142-
public void test043AmazonCaCertsAreInTheKeystore() {
143+
public void test041AmazonCaCertsAreInTheKeystore() {
143144
final boolean matches = Arrays.stream(
144145
sh.run("jdk/bin/keytool -cacerts -storepass changeit -list | grep trustedCertEntry").stdout.split("\n")
145146
).anyMatch(line -> line.contains("amazonrootca"));
@@ -251,8 +252,8 @@ public void test080ConfigurePasswordThroughEnvironmentVariableFile() throws Exce
251252
waitForElasticsearch("green", null, installation, "elastic", "hunter2");
252253
} catch (Exception e) {
253254
throw new AssertionError(
254-
"Failed to check whether Elasticsearch had started. This could be because authentication isn't working properly. "
255-
+ "Check the container logs",
255+
"Failed to check whether Elasticsearch had started. This could be because "
256+
+ "authentication isn't working properly. Check the container logs",
256257
e
257258
);
258259
}
@@ -335,8 +336,7 @@ public void test083EnvironmentVariablesUsingFilesHaveCorrectPermissions() throws
335336

336337
Files.write(tempDir.resolve(passwordFilename), "hunter2\n".getBytes(StandardCharsets.UTF_8));
337338

338-
Map<String, String> envVars = new HashMap<>();
339-
envVars.put("ELASTIC_PASSWORD_FILE", "/run/secrets/" + passwordFilename);
339+
Map<String, String> envVars = singletonMap("ELASTIC_PASSWORD_FILE", "/run/secrets/" + passwordFilename);
340340

341341
// Set invalid file permissions
342342
Files.setPosixFilePermissions(tempDir.resolve(passwordFilename), p660);
@@ -484,7 +484,6 @@ public void test101AllFilesAreGroupZero() {
484484

485485
/**
486486
* Check that the Docker image has the expected "Label Schema" labels.
487-
*
488487
* @see <a href="http://label-schema.org/">Label Schema website</a>
489488
*/
490489
public void test110OrgLabelSchemaLabels() throws Exception {
@@ -526,7 +525,6 @@ public void test110OrgLabelSchemaLabels() throws Exception {
526525

527526
/**
528527
* Check that the Docker image has the expected "Open Containers Annotations" labels.
529-
*
530528
* @see <a href="https://github.com/opencontainers/image-spec/blob/master/annotations.md">Open Containers Annotations</a>
531529
*/
532530
public void test110OrgOpencontainersLabels() throws Exception {
@@ -577,10 +575,10 @@ public void test120DockerLogsIncludeElasticsearchLogs() throws Exception {
577575
}
578576

579577
/**
580-
* Check that the Java process running inside the container has the expect PID, UID and username.
578+
* Check that the Java process running inside the container has the expected UID, GID and username.
581579
*/
582-
public void test130JavaHasCorrectPidAndOwnership() {
583-
final List<String> processes = Arrays.stream(sh.run("ps -o pid,uid,user -C java").stdout.split("\n"))
580+
public void test130JavaHasCorrectOwnership() {
581+
final List<String> processes = Arrays.stream(sh.run("ps -o uid,gid,user -C java").stdout.split("\n"))
584582
.skip(1)
585583
.collect(Collectors.toList());
586584

@@ -589,11 +587,34 @@ public void test130JavaHasCorrectPidAndOwnership() {
589587
final String[] fields = processes.get(0).trim().split("\\s+");
590588

591589
assertThat(fields, arrayWithSize(3));
592-
assertThat("Incorrect PID", fields[0], equalTo("1"));
593-
assertThat("Incorrect UID", fields[1], equalTo("1000"));
590+
assertThat("Incorrect UID", fields[0], equalTo("1000"));
591+
assertThat("Incorrect GID", fields[1], equalTo("0"));
594592
assertThat("Incorrect username", fields[2], equalTo("elasticsearch"));
595593
}
596594

595+
/**
596+
* Check that the init process running inside the container has the expected PID, UID, GID and user.
597+
* The PID is particularly important because PID 1 handles signal forwarding and child reaping.
598+
*/
599+
public void test131InitProcessHasCorrectPID() {
600+
final List<String> processes = Arrays.stream(sh.run("ps -o pid,uid,gid,command -p 1").stdout.split("\n"))
601+
.skip(1)
602+
.collect(Collectors.toList());
603+
604+
assertThat("Expected a single process", processes, hasSize(1));
605+
606+
final String[] fields = processes.get(0).trim().split("\\s+", 4);
607+
608+
assertThat(fields, arrayWithSize(4));
609+
assertThat("Incorrect PID", fields[0], equalTo("1"));
610+
assertThat("Incorrect UID", fields[1], equalTo("0"));
611+
assertThat("Incorrect GID", fields[2], equalTo("0"));
612+
assertThat("Incorrect init command", fields[3], startsWith("/tini"));
613+
}
614+
615+
/**
616+
* Check that Elasticsearch reports per-node cgroup information.
617+
*/
597618
public void test140CgroupOsStatsAreAvailable() throws Exception {
598619
waitForElasticsearch(installation);
599620

0 commit comments

Comments
 (0)