2020
2121package land .oras .auth ;
2222
23+ import com .fasterxml .jackson .annotation .JsonCreator ;
2324import com .fasterxml .jackson .annotation .JsonProperty ;
25+ import com .fasterxml .jackson .annotation .JsonValue ;
2426import java .nio .file .Files ;
2527import java .nio .file .Path ;
2628import java .util .Collections ;
@@ -132,13 +134,52 @@ static ParsedPrefix parse(String prefix) {
132134 }
133135 }
134136
137+ /**
138+ * The for handling short name
139+ */
140+ enum ShortNameMode {
141+
142+ /**
143+ * Use all unqualified-search registries without any restriction
144+ */
145+ DISABLED ("disabled" ),
146+
147+ /**
148+ * If only one unqualified-search registry is set, use it as there is no ambiguity.
149+ * If there is more than one registry this throw an error (default)
150+ */
151+ ENFORCING ("enforcing" ),
152+
153+ /**
154+ * Same as enforcing for ORAS Java SDK
155+ */
156+ PERMISSIVE ("permissive" );
157+
158+ ShortNameMode (String value ) {
159+ this .value = value ;
160+ }
161+
162+ @ JsonCreator
163+ public static ShortNameMode fromString (String key ) {
164+ return ShortNameMode .valueOf (key .toUpperCase ());
165+ }
166+
167+ @ JsonValue
168+ public String getKey () {
169+ return value ;
170+ }
171+
172+ private String value ;
173+ }
174+
135175 /**
136176 * The model of the configuration file, which contains the list of registry configurations, aliases, and unqualified registries.
137177 * @param registries The list of registry configurations, each containing the registry location, whether it is blocked, and whether it is insecure.
138178 * @param aliases The map of registry aliases, where the key is the alias and the value is the actual registry URL.
139179 * @param unqualifiedRegistries The list of unqualified registries, which are registries that can be used without specifying a registry.
140180 */
141181 record ConfigFile (
182+ @ JsonProperty ("short-name-mode" ) @ Nullable ShortNameMode shortNameMode ,
142183 @ JsonProperty ("registry" ) @ Nullable List <RegistryConfig > registries ,
143184 @ JsonProperty ("aliases" ) @ Nullable Map <String , String > aliases ,
144185 @ JsonProperty ("unqualified-search-registries" ) @ Nullable List <String > unqualifiedRegistries ) {}
@@ -151,6 +192,23 @@ public List<String> getUnqualifiedRegistries() {
151192 return Collections .unmodifiableList (config .unqualifiedRegistries );
152193 }
153194
195+ /**
196+ * Enforce the short name mode by checking the configuration. If the short name mode is set to ENFORCING or PERMISSIVE and there are multiple unqualified registries configured, this method throws an OrasException indicating that the configuration is invalid. If the configuration is valid, this method does nothing.
197+ * @throws OrasException if the short name mode is set to ENFORCING or PERMISSIVE and there are multiple unqualified registries configured, indicating that the configuration is invalid.
198+ */
199+ public void enforceShortNameMode () throws OrasException {
200+ if ((config .shortNameMode == ShortNameMode .ENFORCING || config .shortNameMode == ShortNameMode .PERMISSIVE )
201+ && config .unqualifiedRegistries .size () > 1 ) {
202+ throw new OrasException (
203+ "Short name mode is set to ENFORCING/PERMISSION but multiple unqualified registries are configured: "
204+ + config .unqualifiedRegistries );
205+ }
206+ LOG .debug (
207+ "Short name mode '{}' is valid with unqualified registries: {}" ,
208+ config .shortNameMode ,
209+ config .unqualifiedRegistries );
210+ }
211+
154212 /**
155213 * Return the aliases
156214 * @return an unmodifiable map of aliases, where the key is the alias and the value is the actual registry URL.
@@ -315,6 +373,11 @@ private Config() {}
315373 this .registries .addAll (registryConfigs );
316374 }
317375
376+ /**
377+ * Default to enforcing
378+ */
379+ private ShortNameMode shortNameMode = ShortNameMode .ENFORCING ;
380+
318381 /**
319382 * List of unqualified registries.
320383 */
@@ -351,6 +414,10 @@ public static Config load(ConfigFile configFile) throws OrasException {
351414 LOG .trace ("Loading registry configurations: {}" , configFile .registries );
352415 config .registries .addAll (configFile .registries );
353416 }
417+ if (configFile .shortNameMode != null ) {
418+ LOG .trace ("Loading short name mode: {}" , configFile .shortNameMode );
419+ config .shortNameMode = configFile .shortNameMode ;
420+ }
354421 return config ;
355422 }
356423 }
0 commit comments