Skip to content

Commit

Permalink
Update zwes1606w message for troubleshooting
Browse files Browse the repository at this point in the history
Signed-off-by: 1000TurquoisePogs <[email protected]>
  • Loading branch information
1000TurquoisePogs committed Nov 2, 2023
1 parent c85e374 commit d46c6b2
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 41 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

All notable changes to the ZSS package will be documented in this file.

## Recent Changes
## `2.13.0`
- Bugfix: Corrected build environment file's use of IP address to github.com (#660)
- Enhancement: Consolidate JWK warnings into improved ZWES1606W message (#663)

## `2.10.0`
- This action making a CHANGELOG note via special syntax from the GitHub PR commit message, like it could automatically update CHANGELOG.md with the message. First job checks if PR body has changelog note or not if it's not there then it asked them to add it and second job is to check if changelog note has been added in changelog.md file or not. (#636)
Expand Down
113 changes: 76 additions & 37 deletions c/jwk.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,32 @@ static int jwkTaskMain(RLETask *task) {
JwkSettings *settings = context->settings;
const int maxAttempts = 1000;
const int retryIntervalSeconds = settings->retryIntervalSeconds;
const int warnInterval = 10;
bool success = false;

int rc = 0;
int rsn = 0;

for (int i = 0; i < maxAttempts; i++) {
int status = getJwk(context);
if (status == JWK_STATUS_OK) {
getJwk(context, &rc, &rsn);
if (rc == JWK_STATUS_OK) {
success = true;
context->isPublicKeyInitialized = true;
break;
} else if (status == JWK_STATUS_UNRECOGNIZED_FMT_ERROR) {
} else if (rc == JWK_STATUS_UNRECOGNIZED_FMT_ERROR) {
zowelog(NULL, LOG_COMP_ID_JWK, ZOWE_LOG_WARNING, ZSS_LOG_JWK_UNRECOGNIZED_MSG);
break;
} else if (status == JWK_STATUS_PUBLIC_KEY_ERROR) {
} else if (rc == JWK_STATUS_PUBLIC_KEY_ERROR) {
zowelog(NULL, LOG_COMP_ID_JWK, ZOWE_LOG_WARNING, ZSS_LOG_JWK_PUBLIC_KEY_ERROR_MSG);
break;
} else if (status == JWK_STATUS_HTTP_CONTEXT_ERROR) {
} else if (rc == JWK_STATUS_HTTP_CONTEXT_ERROR) {
zowelog(NULL, LOG_COMP_ID_JWK, ZOWE_LOG_WARNING, ZSS_LOG_JWK_HTTP_CTX_ERROR_MSG);
break;
} else {
zowelog(NULL, LOG_COMP_ID_JWK, ZOWE_LOG_WARNING, ZSS_LOG_JWK_RETRY_MSG,
jwkGetStrStatus(status), retryIntervalSeconds);
if (i % warnInterval == 0) {
zowelog(NULL, LOG_COMP_ID_JWK, ZOWE_LOG_WARNING, ZSS_LOG_JWK_RETRY_MSG,
jwkGetStrStatus(rc), rc, jwkHttpClientGetStrStatus(rsn), rsn, retryIntervalSeconds);
}
sleep(retryIntervalSeconds);
}
}
Expand All @@ -119,18 +125,17 @@ static int jwkTaskMain(RLETask *task) {

static int getJwk(JwkContext *context) {
JwkSettings *settings = context->settings;
int status = 0;
int rc = 0;
int rsn = 0;
ShortLivedHeap *slh = makeShortLivedHeap(0x40000, 0x40);

HttpClientSettings clientSettings = {0};
clientSettings.host = settings->host;
clientSettings.port = settings->port;
clientSettings.recvTimeoutSeconds = (settings->timeoutSeconds > 0) ? settings->timeoutSeconds : 10;

Json *jwkJson = doRequest(slh, &clientSettings, settings->tlsEnv, settings->path, &status);
if (status) {
zowelog(NULL, LOG_COMP_ID_JWK, ZOWE_LOG_WARNING, "failed to obtain JWK, status = %d\n", status);
} else {
Json *jwkJson = doRequest(slh, &clientSettings, settings->tlsEnv, settings->path, &rc, &rsn);
if (!rc) {
x509_public_key_info publicKey;
getPublicKey(jwkJson, &publicKey, &status);
if (status == 0) {
Expand All @@ -141,8 +146,8 @@ static int getJwk(JwkContext *context) {
return status;
}

static Json *doRequest(ShortLivedHeap *slh, HttpClientSettings *clientSettings, TlsEnvironment *tlsEnv, char *path, int *statusOut) {
int status = 0;
static Json *doRequest(ShortLivedHeap *slh, HttpClientSettings *clientSettings, TlsEnvironment *tlsEnv, char *path, int *rc, int *rsn) {
*rsn = 0;
HttpClientContext *httpClientContext = NULL;
HttpClientSession *session = NULL;
LoggingContext *loggingContext = makeLoggingContext();
Expand All @@ -151,40 +156,39 @@ static Json *doRequest(ShortLivedHeap *slh, HttpClientSettings *clientSettings,
do {
zowelog(NULL, LOG_COMP_ID_JWK, ZOWE_LOG_DEBUG, "JWK request to https://%s:%d%s\n",
clientSettings->host, clientSettings->port, path);
status = httpClientContextInitSecure(clientSettings, loggingContext, tlsEnv, &httpClientContext);
if (status) {
zowelog(NULL, LOG_COMP_ID_JWK, ZOWE_LOG_WARNING, "error in httpcb ctx init: %d\n", status);
*statusOut = JWK_STATUS_HTTP_CONTEXT_ERROR;
*rsn = httpClientContextInitSecure(clientSettings, loggingContext, tlsEnv, &httpClientContext);
if (*rsn) {
*rc = JWK_STATUS_HTTP_CONTEXT_ERROR;
break;
}
status = httpClientSessionInit(httpClientContext, &session);
if (status) {
zowelog(NULL, LOG_COMP_ID_JWK, ZOWE_LOG_WARNING, "error initing session: %d\n", status);
*statusOut = JWK_STATUS_HTTP_REQUEST_ERROR;
*rsn = httpClientSessionInit(httpClientContext, &session);
if (*rsn) {
zowelog(NULL, LOG_COMP_ID_JWK, ZOWE_LOG_WARNING, "error initing session: %d\n", *rsn);
*rc = JWK_STATUS_HTTP_REQ_INIT_ERROR;
break;
}
status = httpClientSessionStageRequest(httpClientContext, session, "GET", path, NULL, NULL, NULL, 0);
if (status) {
zowelog(NULL, LOG_COMP_ID_JWK, ZOWE_LOG_WARNING, "error staging request: %d\n", status);
*statusOut = JWK_STATUS_HTTP_REQUEST_ERROR;
*rsn = httpClientSessionStageRequest(httpClientContext, session, "GET", path, NULL, NULL, NULL, 0);
if (*rsn) {
zowelog(NULL, LOG_COMP_ID_JWK, ZOWE_LOG_WARNING, "error staging request: %d\n", *rsn);
*rc = JWK_STATUS_HTTP_REQ_STAGING_ERROR;
break;
}
requestStringHeader(session->request, TRUE, "accept", "application/json");
status = httpClientSessionSend(httpClientContext, session);
if (status) {
zowelog(NULL, LOG_COMP_ID_JWK, ZOWE_LOG_WARNING, "error sending request: %d\n", status);
*statusOut = JWK_STATUS_HTTP_REQUEST_ERROR;
*rsn = httpClientSessionSend(httpClientContext, session);
if (*rsn) {
zowelog(NULL, LOG_COMP_ID_JWK, ZOWE_LOG_WARNING, "error sending request: %d\n", *rsn);
*rc = JWK_STATUS_HTTP_REQ_SEND_ERROR;
break;
}
jsonBody = receiveResponse(slh, httpClientContext, session, &status);
if (status) {
*statusOut = status;
jsonBody = receiveResponse(slh, httpClientContext, session, rc);
if (*rc) {
*rsn = *rc;
break;
}
int statusCode = session->response->statusCode;
if (statusCode != 200) {
zowelog(NULL, LOG_COMP_ID_JWK, ZOWE_LOG_WARNING, "HTTP status %d\n", statusCode);
*statusOut = JWK_STATUS_RESPONSE_ERROR;
*rc = JWK_STATUS_RESPONSE_ERROR;
break;
}
} while (0);
Expand Down Expand Up @@ -372,18 +376,53 @@ static const char *MESSAGES[] = {
[JWK_STATUS_UNRECOGNIZED_FMT_ERROR] = "JWK is in unrecognized format",
[JWK_STATUS_PUBLIC_KEY_ERROR] = "failed to create public key",
[JWK_STATUS_HTTP_CONTEXT_ERROR] = "failed to init HTTP context",
[JWK_STATUS_HTTP_REQUEST_ERROR] = "failed to send HTTP request"
[JWK_STATUS_HTTP_REQ_INIT_ERROR] = "failed to init HTTP request",
[JWK_STATUS_HTTP_REQ_STAGING_ERROR] = "failed on staging HTTP request",
[JWK_STATUS_HTTP_REQ_SEND_ERROR] = "failed to send HTTP request"
};

static const char *HTTP_CLIENT_MESSAGES[] = {
[HTTP_CLIENT_INVALID_ARGUMENT] = "Invalid argument to client",
[HTTP_CLIENT_OUTPUT_WOULD_OVERFLOW] = "Output would overflow",
[HTTP_CLIENT_INVALID_PORT] = "Invalid port",
[HTTP_CLIENT_REQDSETTING_MISSING] = "Required setting missing",
[HTTP_CLIENT_LOOKUP_FAILED] = "Lookup failed",
[HTTP_CLIENT_CONNECT_FAILED] = "Connect failed",
[HTTP_CLIENT_SESSION_ERR] = "Client session error",
[HTTP_CLIENT_ADDRBYNAME_ERR] = "Hostname to IP error",
[HTTP_CLIENT_SEND_ERROR] = "Failed to send",
[HTTP_CLIENT_SOCKET_UNREGISTERED] = "Socket unregistered",
[HTTP_CLIENT_SXREAD_ERROR] = "SelectX Read Error",
[HTTP_CLIENT_NO_REQUEST] = "No request",
[HTTP_CLIENT_NO_SOCKET] = "No Socket",
[HTTP_CLIENT_RESP_PARSE_FAILED] = "Response parsing failed",
[HTTP_CLIENT_READ_ERROR] = "Read error",
[HTTP_CLIENT_RESPONSE_ZEROLEN] = "Response is zero length",
[HTTP_CLIENT_TLS_ERROR] = "TLS error",
[HTTP_CLIENT_TLS_NOT_CONFIGURED] = "TLS not configured",
};


#define MESSAGE_COUNT sizeof(MESSAGES)/sizeof(MESSAGES[0])

const char *jwkGetStrStatus(int status) {
if (status >= MESSAGE_COUNT || status < 0) {
return "Unknown status code";
return "Unknown rc";
}
const char *message = MESSAGES[status];
if (!message) {
return "Unknown status code";
return "Unknown rc";
}
return message;
}

const char *jwkHttpClientGetStrStatus(int status) {
if (status >= HTTP_CLIENT_TLS_NOT_CONFIGURED || status < 0) {
return "Unknown reason";
}
const char *message = HTTP_CLIENT_MESSAGES[status];
if (!message) {
return "Unknown reason";
}
return message;
}
Expand Down
2 changes: 1 addition & 1 deletion deps/zowe-common-c
4 changes: 3 additions & 1 deletion h/jwk.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ struct JwkContext_tag {
#define JWK_STATUS_UNRECOGNIZED_FMT_ERROR 4
#define JWK_STATUS_PUBLIC_KEY_ERROR 7
#define JWK_STATUS_HTTP_CONTEXT_ERROR 8
#define JWK_STATUS_HTTP_REQUEST_ERROR 9
#define JWK_STATUS_HTTP_REQ_INIT_ERROR 9
#define JWK_STATUS_HTTP_REQ_STAGING_ERROR 10
#define JWK_STATUS_HTTP_REQ_SEND_ERROR 11

void configureJwt(HttpServer *server, JwkSettings *jwkSettings);
const char *jwkGetStrStatus(int status);
Expand Down
2 changes: 1 addition & 1 deletion h/zssLogging.h
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ bool isLogLevelValid(int level);
#ifndef ZSS_LOG_JWK_RETRY_MSG_ID
#define ZSS_LOG_JWK_RETRY_MSG_ID ZSS_LOG_MSG_PRFX"1606W"
#endif
#define ZSS_LOG_JWK_RETRY_MSG_TEXT "Failed to get JWK - %s, retry in %d seconds\n"
#define ZSS_LOG_JWK_RETRY_MSG_TEXT "Failed to get JWK. rc=%s (%d), rsn=%s (%d). Retry in %d seconds\n"
#define ZSS_LOG_JWK_RETRY_MSG ZSS_LOG_JWK_RETRY_MSG_ID" "ZSS_LOG_JWK_RETRY_MSG_TEXT

#endif /* MVD_H_ZSSLOGGING_H_ */
Expand Down

0 comments on commit d46c6b2

Please sign in to comment.