Skip to content

Commit 21aa905

Browse files
authored
Added support for custom userid groupid file ownership (#39)
1 parent 3387efd commit 21aa905

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/CreateImage.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.logging.Logger;
2222
import java.util.stream.Collectors;
2323

24-
import com.oracle.weblogic.imagetool.api.model.CachePolicy;
2524
import com.oracle.weblogic.imagetool.api.model.CommandResponse;
2625
import com.oracle.weblogic.imagetool.api.model.DomainType;
2726
import com.oracle.weblogic.imagetool.api.model.InstallerType;

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/menu/ImageOperation.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import java.util.List;
1414
import java.util.concurrent.Callable;
1515
import java.util.logging.Logger;
16+
import java.util.regex.Matcher;
17+
import java.util.regex.Pattern;
1618
import java.util.stream.Collectors;
1719
import java.util.stream.Stream;
1820

@@ -61,6 +63,9 @@ public CommandResponse call() throws Exception {
6163
return new CommandResponse(-1, "user Oracle support credentials do not match");
6264
}
6365
}
66+
67+
handleChown();
68+
6469
logger.finer("Exiting ImageOperation call ");
6570
return new CommandResponse(0, null);
6671
}
@@ -188,6 +193,28 @@ void addOPatch1394ToImage(Path tmpDir, String opatchBugNumber) throws Exception
188193
dockerfileOptions.setOPatchPatchingEnabled();
189194
}
190195

196+
private void handleChown() {
197+
if (osUserAndGroup.length != 2) {
198+
throw new IllegalArgumentException("--chown value must be a colon separated user and group. user:group");
199+
}
200+
201+
Pattern p = Pattern.compile("^[a-z_]([a-z0-9_-]{0,31}|[a-z0-9_-]{0,30}\\$)$");
202+
Matcher usr = p.matcher(osUserAndGroup[0]);
203+
if (!usr.matches()) {
204+
throw new IllegalArgumentException("--chown must contain a valid Unix username. No more than 32 characters"
205+
+ " and starts with a lowercase character. Invalid value = " + osUserAndGroup[0]);
206+
}
207+
Matcher grp = p.matcher(osUserAndGroup[1]);
208+
if (!grp.matches()) {
209+
throw new IllegalArgumentException("--chown must contain a valid Unix groupid. No more than 32 characters"
210+
+ " and starts with a lowercase character. Invalid value = " + osUserAndGroup[1]);
211+
}
212+
213+
dockerfileOptions.setUserId(osUserAndGroup[0]);
214+
dockerfileOptions.setGroupId(osUserAndGroup[1]);
215+
}
216+
217+
191218
WLSInstallerType installerType = WLSInstallerType.WLS;
192219

193220
String installerVersion = Constants.DEFAULT_WLS_VERSION;
@@ -277,6 +304,14 @@ void addOPatch1394ToImage(Path tmpDir, String opatchBugNumber) throws Exception
277304
)
278305
Path dockerLog;
279306

307+
@Option(
308+
names = {"--chown"},
309+
split = ":",
310+
description = "userid:groupid for JDK/Middleware installs and patches. Default: ${DEFAULT-VALUE}.",
311+
defaultValue = "oracle:oracle"
312+
)
313+
private String[] osUserAndGroup;
314+
280315
@Unmatched
281316
List<String> unmatchedOptions;
282317
}

imagetool/src/main/resources/docker-files/Create_Image.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ RUN zypper -nq update \
3030
{{/useZypper}}
3131

3232
## Create user and group
33-
RUN if [ -z "$(getent group {{groupid}})" ]; then hash groupadd &> /dev/null && groupadd -g 1000 {{groupid}} || exit -1 ; fi \
34-
&& if [ -z "$(getent passwd {{userid}})" ]; then hash useradd &> /dev/null && useradd -u 1100 -g {{groupid}} {{userid}} || exit -1; fi \
33+
RUN if [ -z "$(getent group {{groupid}})" ]; then hash groupadd &> /dev/null && groupadd {{groupid}} || exit -1 ; fi \
34+
&& if [ -z "$(getent passwd {{userid}})" ]; then hash useradd &> /dev/null && useradd -g {{groupid}} {{userid}} || exit -1; fi \
3535
&& mkdir /u01 \
3636
&& chown {{userid}}:{{groupid}} /u01
3737

@@ -198,8 +198,8 @@ ENV DOMAIN_HOME=${DOMAIN_HOME:-/u01/domains/base_domain} \
198198
PATH=$PATH:{{java_home}}/bin:${ORACLE_HOME}/oracle_common/common/bin:${ORACLE_HOME}/wlserver/common/bin:${DOMAIN_HOME}/bin:${ORACLE_HOME}
199199

200200
## Create user and group
201-
RUN if [ -z "$(getent group {{groupid}})" ]; then hash groupadd &> /dev/null && groupadd -g 1000 {{groupid}} || exit -1 ; fi \
202-
&& if [ -z "$(getent passwd {{userid}})" ]; then hash useradd &> /dev/null && useradd -u 1100 -g {{groupid}} {{userid}} || exit -1; fi \
201+
RUN if [ -z "$(getent group {{groupid}})" ]; then hash groupadd &> /dev/null && groupadd {{groupid}} || exit -1 ; fi \
202+
&& if [ -z "$(getent passwd {{userid}})" ]; then hash useradd &> /dev/null && useradd -g {{groupid}} {{userid}} || exit -1; fi \
203203
&& mkdir -p $(dirname {{java_home}}) $(dirname $ORACLE_HOME) $(dirname $DOMAIN_HOME) \
204204
&& chown {{userid}}:{{groupid}} $(dirname {{java_home}}) $(dirname $ORACLE_HOME) $(dirname $DOMAIN_HOME)
205205

imagetool/src/main/resources/docker-files/Update_Image.mustache

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ ENV PATCHDIR=${PATCHDIR:-patches} \
1515
OTMPDIR=${OTMPDIR:-/tmp/delme} \
1616
OPATCH_NO_FUSER=true
1717

18-
USER oracle
18+
USER {{userid}}
1919

2020
{{#isOpatchPatchingEnabled}}
21-
COPY --chown=oracle:oracle p28186730_139400_Generic.zip $OTMPDIR/opatch/
21+
COPY --chown={{userid}}:{{groupid}} p28186730_139400_Generic.zip $OTMPDIR/opatch/
2222
RUN cd $OTMPDIR/opatch \
2323
&& $JAVA_HOME/bin/jar -xf $OTMPDIR/opatch/p28186730_139400_Generic.zip \
2424
&& $JAVA_HOME/bin/java -jar $OTMPDIR/opatch/6880880/opatch_generic.jar -silent -ignoreSysPrereqs -force -novalidation oracle_home=$ORACLE_HOME \
2525
&& rm -rf $OTMPDIR
2626
{{/isOpatchPatchingEnabled}}
2727

2828
{{#isPatchingEnabled}}
29-
COPY --chown=oracle:oracle $PATCHDIR/* $OTMPDIR/patches/
29+
COPY --chown={{userid}}:{{groupid}} $PATCHDIR/* $OTMPDIR/patches/
3030

3131
RUN $ORACLE_HOME/OPatch/opatch napply -silent -oh $ORACLE_HOME -phBaseDir $OTMPDIR/patches \
3232
&& $ORACLE_HOME/OPatch/opatch util cleanup -silent -oh $ORACLE_HOME \

0 commit comments

Comments
 (0)