Skip to content

Commit 092ed36

Browse files
committed
fix: let unused unreadable ABFS keys pass the configuration check
Deciding whether an account has ABFS authentication configured read every auth key through Hadoop. A key the selected mechanism never uses, such as an OAuth secret with a cyclic ${...} reference next to a valid SharedKey, threw there and failed the scan although hadoop-azure initializes fine. A key whose read throws inside Hadoop now counts as configured. The chosen mechanism reads only its own keys, so a broken key it needs still fails in Hadoop's words.
1 parent 8f6d361 commit 092ed36

3 files changed

Lines changed: 87 additions & 22 deletions

File tree

‎spark/src/main/scala/org/apache/comet/objectstore/AbfsAuthResolver.scala‎

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,6 @@ private[comet] object AbfsAuthResolver extends Logging {
278278
account: String,
279279
secrets: ArrayBuffer[String]) {
280280

281-
def get(key: String): Option[String] = h.get(abfsConf, key)
282-
283281
def password(key: String): Option[String] = {
284282
val value = h.getPasswordString(abfsConf, key)
285283
value.foreach(v => recordSecret(v))
@@ -303,24 +301,29 @@ private[comet] object AbfsAuthResolver extends Logging {
303301
def isConfigured: Boolean = {
304302
// Every credential is read (no short circuit) so a later failure message is redacted
305303
// against all of them.
306-
val anyPassword = passwordAuthKeys.map(password).exists(_.isDefined)
307-
val keyProviderKey = keyProviderAccountKey.isDefined
308-
plainAuthKeys.exists(get(_).isDefined) || anyPassword || keyProviderKey
304+
val anyPassword = passwordAuthKeys.map(isSet(abfsConf, _, password = true)).contains(true)
305+
val keyProviderKey = hasKeyProviderAccountKey
306+
plainAuthKeys.exists(isSet(abfsConf, _, password = false)) || anyPassword || keyProviderKey
309307
}
310308

309+
// A value Hadoop cannot read still counts as set: Hadoop reads a key only when the mechanism
310+
// it picks needs it, and then fails in its own words.
311+
private def isSet(conf: AnyRef, key: String, password: Boolean): Boolean =
312+
h.read(conf, key, password) match {
313+
case Right(value) =>
314+
if (password) value.foreach(v => recordSecret(v))
315+
value.isDefined
316+
case Left(_) => true
317+
}
318+
311319
// SimpleKeyProvider reads the key through its own two-argument AbfsConfiguration, whose
312320
// container level on 3.4.2+ is the literal `key.null.<account>`; a key placed there
313321
// configures the account for Hadoop, so it counts here too.
314-
private def keyProviderAccountKey: Option[String] = {
315-
if (!h.hasContainerScopedConfiguration) {
316-
None
317-
} else {
318-
val keyProviderConf = h.newKeyProviderConfiguration(hadoopConf, account)
319-
val value = h.getPasswordString(keyProviderConf, Keys.ACCOUNT_KEY)
320-
value.foreach(v => recordSecret(v))
321-
value
322-
}
323-
}
322+
private def hasKeyProviderAccountKey: Boolean =
323+
h.hasContainerScopedConfiguration && isSet(
324+
h.newKeyProviderConfiguration(hadoopConf, account),
325+
Keys.ACCOUNT_KEY,
326+
password = true)
324327
}
325328

326329
private def sharedKey(session: Session, uri: URI, account: String): Outcome = {

‎spark/src/main/scala/org/apache/comet/objectstore/AbfsReflection.scala‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,20 @@ private[objectstore] object AbfsReflection {
9090
/** `getAuthType(account)`: the `AuthType` enum constant. */
9191
def getAuthType(abfsConf: AnyRef, account: String): Enum[_]
9292

93-
/** `get(key)`: account-specific, then global. */
94-
def get(abfsConf: AnyRef, key: String): Option[String]
95-
9693
/**
9794
* `getPasswordString(key)`: container (3.4.2+), account, then global, via credential
9895
* providers.
9996
*/
10097
def getPasswordString(abfsConf: AnyRef, key: String): Option[String]
10198

99+
/**
100+
* `getPasswordString(key)` when `password` is set, otherwise `get(key)` (account-specific,
101+
* then global). A value Hadoop cannot read (a `${...}` cycle, a failing credential provider)
102+
* is returned as `Left` rather than thrown; fatal errors and linkage or reflection failures
103+
* still propagate.
104+
*/
105+
def read(abfsConf: AnyRef, key: String, password: Boolean): Either[Throwable, Option[String]]
106+
102107
/**
103108
* `getTokenProviderClass(authType, key, null, xface)`: the provider class Hadoop would load.
104109
*/
@@ -222,12 +227,24 @@ private[objectstore] object AbfsReflection {
222227
override def getAuthType(abfsConf: AnyRef, account: String): Enum[_] =
223228
invoke(getAuthTypeMethod, abfsConf, account).asInstanceOf[Enum[_]]
224229

225-
override def get(abfsConf: AnyRef, key: String): Option[String] =
226-
Option(invoke(getMethod, abfsConf, key)).map(_.asInstanceOf[String])
227-
228230
override def getPasswordString(abfsConf: AnyRef, key: String): Option[String] =
229231
Option(invoke(getPasswordStringMethod, abfsConf, key)).map(_.asInstanceOf[String])
230232

233+
override def read(
234+
abfsConf: AnyRef,
235+
key: String,
236+
password: Boolean): Either[Throwable, Option[String]] = {
237+
val method = if (password) getPasswordStringMethod else getMethod
238+
try {
239+
Right(Option(method.invoke(abfsConf, key)).map(_.asInstanceOf[String]))
240+
} catch {
241+
// NonFatal excludes LinkageError, which rethrows below like any other failure.
242+
case e: InvocationTargetException if e.getCause != null && NonFatal(e.getCause) =>
243+
Left(e.getCause)
244+
case e: InvocationTargetException => throw unwrapped(e)
245+
}
246+
}
247+
231248
override def getTokenProviderClass(
232249
abfsConf: AnyRef,
233250
authType: Enum[_],

‎spark/src/test/scala/org/apache/comet/objectstore/AbfsAuthResolverSuite.scala‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ class AbfsAuthResolverSuite extends AnyFunSuite with Matchers {
6969

7070
private def resolveWith(pairs: (String, String)*): Outcome = resolve(conf(pairs: _*), uri)
7171

72+
/**
73+
* `key` set to a `${...}` cycle through `other`, which `Configuration#get` cannot expand and
74+
* throws on. Built by concatenation so scalac does not read it as a missing interpolator.
75+
*/
76+
private def cyclic(key: String, other: String): Seq[(String, String)] =
77+
Seq(key -> ("${" + other + "}"), other -> ("${" + key + "}"))
78+
7279
private def assertNoAzureKeys(options: Map[String, String]): Unit = {
7380
val leaked = options.keys.filter(_.startsWith("fs."))
7481
assert(leaked.isEmpty, s"fs.* keys forwarded: $leaked")
@@ -357,6 +364,41 @@ class AbfsAuthResolverSuite extends AnyFunSuite with Matchers {
357364
Keys.ACCOUNT_KEY -> "c2VjcmV0")
358365
}
359366

367+
test("Resolved - SharedKey with an unreadable OAuth client secret it never reads") {
368+
val outcome = resolveWith(
369+
Seq(acct(Keys.ACCOUNT_KEY) -> "c2VjcmV0") ++
370+
cyclic(Keys.CLIENT_SECRET, "unused.secret"): _*)
371+
outcome shouldBe Resolved("SharedKey", None, Map(Keys.ACCOUNT_KEY -> "c2VjcmV0"))
372+
}
373+
374+
test("Resolved - SharedKey with an unreadable OAuth provider type it never reads") {
375+
val outcome = resolveWith(
376+
Seq(acct(Keys.ACCOUNT_KEY) -> "c2VjcmV0") ++
377+
cyclic(Keys.OAUTH_PROVIDER_TYPE, "unused.provider"): _*)
378+
outcome shouldBe Resolved("SharedKey", None, Map(Keys.ACCOUNT_KEY -> "c2VjcmV0"))
379+
}
380+
381+
test("not Unconfigured - an unreadable key alone fails in Hadoop's SharedKey words") {
382+
Seq(Keys.CLIENT_SECRET, Keys.OAUTH_PROVIDER_TYPE).foreach { key =>
383+
withClue(s"$key alone: ") {
384+
val outcome = resolveWith(cyclic(key, "unused.value"): _*)
385+
outcome shouldBe a[Failed]
386+
outcome.asInstanceOf[Failed].exceptionClass shouldBe keyProviderException
387+
}
388+
}
389+
}
390+
391+
test("Failed - an unreadable account key fails whether or not SharedKey is named") {
392+
Seq(Seq.empty[(String, String)], Seq(acct(Keys.AUTH_TYPE) -> "SharedKey")).foreach {
393+
authType =>
394+
withClue(s"auth type $authType: ") {
395+
val outcome = resolveWith(authType ++ cyclic(acct(Keys.ACCOUNT_KEY), "cycle.key"): _*)
396+
outcome shouldBe a[Failed]
397+
assertNoAzureKeys(toOptions(outcome))
398+
}
399+
}
400+
}
401+
360402
test("Resolved - SharedKey from the global key, with a port-scoped account") {
361403
resolveWith(Keys.ACCOUNT_KEY -> "Z2xvYmFs") shouldBe
362404
Resolved("SharedKey", None, Map(Keys.ACCOUNT_KEY -> "Z2xvYmFs"))
@@ -590,8 +632,11 @@ object LinkageStub extends Handles {
590632
override def newKeyProviderConfiguration(conf: Configuration, account: String): AnyRef =
591633
notReached
592634
override def getAuthType(abfsConf: AnyRef, account: String): Enum[_] = notReached
593-
override def get(abfsConf: AnyRef, key: String): Option[String] = notReached
594635
override def getPasswordString(abfsConf: AnyRef, key: String): Option[String] = notReached
636+
override def read(
637+
abfsConf: AnyRef,
638+
key: String,
639+
password: Boolean): Either[Throwable, Option[String]] = notReached
595640
override def getTokenProviderClass(
596641
abfsConf: AnyRef,
597642
authType: Enum[_],

0 commit comments

Comments
 (0)