Compare commits

..
Author SHA1 Message Date
Viktor ScharfandFlorian Schade 35e4d4a3d6 api-test: retry token refresh on transient IDP failures (#3507)
* api-test: retry token refresh on transient IDP failures

* Fix retry condition in token exchange logic

* test: keep the retry limit a limit and the refresh fallback to exceptions

---------

Co-authored-by: Florian Schade <f.schade@icloud.com>
2026-09-11 14:56:57 +02:00
6 changed files with 59 additions and 25 deletions

No files matched your search

@@ -119,6 +119,9 @@ func (t kqlOpensearchTranspiler) toBuilder(node ast.Node) (osu.Builder, error) {
}
field, value := node.Key, node.Value
if query.FieldIsPath(node.Key) {
value = strings.TrimSuffix(value, "/")
}
if node.CaseInsensitive {
field += mapping.LowercaseSuffix
value = strings.ToLower(value)
-2
View File
@@ -250,8 +250,6 @@ Fixtures:
| PATH-06 | `path:"./DOCUMENTS"` | docs-upper | docs-upper | docs-upper | ✅ |
| PATH-07 | `path:"./Documents"` | docs-mixed | docs-mixed | docs-mixed | ✅ |
| PATH-08 | `path:"./parent/"` | child.jpg, parent | child.jpg, parent | child.jpg, parent | ✅ |
| PATH-09 | `path:"/"` | child.jpg, docs-lower, docs-mixed, docs-upper, parent | child.jpg, docs-lower, docs-mixed, docs-upper, parent | child.jpg, docs-lower, docs-mixed, docs-upper, parent | ✅ |
| PATH-10 | `path:""` | child.jpg, docs-lower, docs-mixed, docs-upper, parent | child.jpg, docs-lower, docs-mixed, docs-upper, parent | child.jpg, docs-lower, docs-mixed, docs-upper, parent | ✅ |
### fields
@@ -23,8 +23,6 @@ func pathGroup() queryGroup {
{id: 6, query: `path:"./DOCUMENTS"`, want: []string{"docs-upper"}},
{id: 7, query: `path:"./Documents"`, want: []string{"docs-mixed"}},
{id: 8, query: `path:"./parent/"`, want: []string{"parent", "child.jpg"}},
{id: 9, query: `path:"/"`, want: []string{"parent", "child.jpg", "docs-lower", "docs-upper", "docs-mixed"}},
{id: 10, query: `path:""`, want: []string{"parent", "child.jpg", "docs-lower", "docs-upper", "docs-mixed"}},
},
}
}
@@ -95,6 +95,9 @@ func walk(offset int, nodes []ast.Node) (bleveQuery.Query, int, error) {
// bleve treats `/` and `+` as literals mid-term, so a literal MIME like
// image/svg+xml still matches exactly.
val := n.Value
if searchQuery.FieldIsPath(n.Key) {
val = strings.TrimSuffix(val, "/")
}
k := n.Key
v := val
if k != "ID" && k != "Size" && k != "MimeType" {
-6
View File
@@ -36,12 +36,6 @@ func normalizeNodes(nodes []ast.Node, resolve func(string) string, defaultKey st
switch node := n.(type) {
case *ast.StringNode:
node.Key = resolveKey(node.Key)
if FieldIsPath(node.Key) {
node.Value = strings.TrimSuffix(node.Value, "/")
if node.Value == "" {
node.Value = "."
}
}
if FieldValueIsNormalized(node.Key) {
node.Value = strings.ToLower(node.Value)
}
+53 -15
View File
@@ -32,10 +32,37 @@ class TokenHelper {
private const LOGON_URL = '/signin/v1/identifier/_/logon';
private const REDIRECT_URL = '/oidc-callback.html';
private const TOKEN_URL = '/konnect/v1/token';
private const TRANSPORT_RETRY_LIMIT = 3;
// Static cache [username => token_data]
private static array $tokenCache = [];
/**
* Run a token exchange and retry it if it fails with a transport error. The
* limit counts retries, the exchange runs at most one time more than that.
*
* @param callable $exchange returns the token data array
*
* @return array
* @throws GuzzleException the last error if every attempt fails
*/
private static function retryOnTransportError(callable $exchange): array {
$attempt = 0;
while (true) {
try {
return $exchange();
} catch (GuzzleException $e) {
if ($attempt >= self::TRANSPORT_RETRY_LIMIT) {
throw $e;
}
$attempt++;
echo "[INFO] token exchange failed with '" . $e->getMessage() .
"', retrying ($attempt)...\n";
\sleep(1);
}
}
}
/**
* @return bool
*/
@@ -80,24 +107,35 @@ class TokenHelper {
return $cachedToken;
}
$refreshedToken = self::refreshToken($cachedToken['refresh_token'], $baseUrl);
$tokenData = [
'access_token' => $refreshedToken['access_token'],
'refresh_token' => $refreshedToken['refresh_token'],
// set expiry to 240 (4 minutes) seconds to allow for some buffer
// token actually expires in 300 seconds (5 minutes)
'expires_at' => time() + 240
];
self::$tokenCache[$cacheKey] = $tokenData;
return $tokenData;
try {
$refreshedToken = self::retryOnTransportError(
fn () => self::refreshToken($cachedToken['refresh_token'], $baseUrl)
);
$tokenData = [
'access_token' => $refreshedToken['access_token'],
'refresh_token' => $refreshedToken['refresh_token'],
// set expiry to 240 (4 minutes) seconds to allow for some buffer
// token actually expires in 300 seconds (5 minutes)
'expires_at' => time() + 240
];
self::$tokenCache[$cacheKey] = $tokenData;
return $tokenData;
} catch (\Exception $e) {
echo "[INFO] token refresh failed with '" . $e->getMessage() .
"', falling back to a full login...\n";
unset(self::$tokenCache[$cacheKey]);
}
}
// Get new tokens
$cookieJar = new CookieJar();
$continueUrl = self::getAuthorizedEndPoint($username, $password, $baseUrl, $cookieJar);
$code = self::getCode($continueUrl, $baseUrl, $cookieJar);
$tokens = self::getToken($code, $baseUrl, $cookieJar);
$tokens = self::retryOnTransportError(
function () use ($username, $password, $baseUrl) {
$cookieJar = new CookieJar();
$continueUrl = self::getAuthorizedEndPoint($username, $password, $baseUrl, $cookieJar);
$code = self::getCode($continueUrl, $baseUrl, $cookieJar);
return self::getToken($code, $baseUrl, $cookieJar);
}
);
$tokenData = [
'access_token' => $tokens['access_token'],