Skip to content

Commit 2fac2b9

Browse files
committed
Added QA build
1 parent afaef98 commit 2fac2b9

7 files changed

Lines changed: 133 additions & 78 deletions

File tree

agent.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,16 @@ extern ScheduledJob_t* currentJob; /* Defined in schedule.c */
4343
#endif
4444

4545
/* Versioning Information */
46-
/* 3.0.0.1 = Release candidate for agent v3 */
46+
/* 3.0.0.2 = Release candidate for agent v3 */
4747
#define AGENT_MAJOR 3ULL
4848
#define AGENT_MINOR 0ULL
4949
#define AGENT_MICRO 0ULL
50-
#define AGENT_BUILD 2ULL
50+
51+
#ifdef __QATESTING__
52+
#define AGENT_BUILD 999ULL // Special build number for QA testing
53+
#else
54+
#define AGENT_BUILD 2ULL
55+
#endif
5156

5257
#define AGENT_VERSION \
5358
((AGENT_MAJOR << 48) | \

httpclient.c

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ static void stripCR(char string[])
158158
}
159159
} /* while */
160160
string[y] = '\0';
161-
return;
162161
}
163162

164163
/******************************************************************************/
@@ -434,23 +433,17 @@ int http_post_json(const char *url, const char *username,
434433
log_trace("%s::%s(%d) : cURL options set correctly", LOG_INF);
435434

436435
struct curl_slist *list = NULL;
437-
/**************************************************************************/
438-
/*
439-
* Set up the HTTP header to tell the API this is standard JSON.
440-
*/
441-
/*
442-
* NOTE: Some versions of Internet Explorer have a problem using
443-
*/
444-
/* these headers. */
445-
/*
446-
* Also, set the content length header option to the data size.
447-
*/
436+
/**********************************************************************/
437+
/* Set up the HTTP header to tell the API this is standard JSON. */
438+
/* NOTE: Some versions of Internet Explorer have a problem using */
439+
/* these headers. */
440+
/* Also, set the content length header option to the data size. */
441+
/**********************************************************************/
448442
/*
449-
* TODO: Error checking, as this is a dynamic memory allocation and
443+
* //TODO: Error checking, as this is a dynamic memory allocation and
450444
* any
445+
* on-demand memory allocation needs a verification step.
451446
*/
452-
/* on-demand memory allocation needs a verification step. */
453-
/**************************************************************************/
454447
list = curl_slist_append(NULL, "Content-Type: application/json");
455448
list = curl_slist_append(list, "Accept: application/json");
456449
char clBuf[30];
@@ -466,11 +459,9 @@ int http_post_json(const char *url, const char *username,
466459
log_debug("%s::%s(%d) : Skipping adding header = %s", LOG_INF, CLIENT_CERT_HEADER);
467460
}
468461

469-
/**************************************************************************/
470-
/*
471-
* Now add the header & data to the HTTP POST request.
472-
*/
473-
/**************************************************************************/
462+
/**********************************************************************/
463+
/* Now add the header & data to the HTTP POST request. */
464+
/**********************************************************************/
474465
errNum = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
475466
if (CURLE_OK != errNum) {
476467
return handle_curl_error(curl, errNum);
@@ -483,24 +474,19 @@ int http_post_json(const char *url, const char *username,
483474
if (CURLE_OK != errNum) {
484475
return handle_curl_error(curl, errNum);
485476
}
477+
#ifdef __QATESTING__
478+
log_qa("%s::%s(%d): postData = %s", LOG_INF, postData);
479+
#else
486480
log_trace("%s::%s(%d): postData = %s", LOG_INF, postData);
481+
#endif
487482

488-
489-
/**************************************************************************/
490-
/*
491-
* Make sure the cURL operation succeeded and the HTTP response code
492-
*/
493-
/*
494-
* indicates success. If we are successfull, place the response
495-
* message
496-
*/
497-
/*
498-
* If the cURL operation fails, return the cURL error code.
499-
*/
500-
/*
501-
* If the HTTP response is an error, return the HTTP failure code.
502-
*/
503-
/**************************************************************************/
483+
/**********************************************************************/
484+
/* Make sure the cURL operation succeeded and the HTTP response code */
485+
/* indicates success. If we are successfull, place the response */
486+
/* message */
487+
/* If the cURL operation fails, return the cURL error code. */
488+
/* If the HTTP response is an error, return the HTTP failure code. */
489+
/**********************************************************************/
504490
long httpCode = 0;
505491
int res = CURLE_FAILED_INIT;
506492
int tries = retryCount;
@@ -542,7 +528,11 @@ int http_post_json(const char *url, const char *username,
542528
log_verbose("%s::%s(%d): %lu bytes retrieved -- allocating memory for response",
543529
LOG_INF, (unsigned long)chunk.size);
544530
*pRespData = strdup(chunk.memory);
531+
#ifdef __QATESTING__
532+
log_qa("%s::%s(%d): Response is:\n%s", LOG_INF, *pRespData);
533+
#else
545534
log_trace("%s::%s(%d): Response is:\n%s", LOG_INF, *pRespData);
535+
#endif
546536
if (NULL == *pRespData) {
547537
log_error("%s::%s(%d): Out of memory", LOG_INF);
548538
toReturn = 255;

logging.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
#define VERBOSELVL "[VERBOSE]"
3131
#define DEBUGLVL "[DEBUG] "
3232
#define TRACELVL "[TRACE] "
33+
#ifdef __QATESTING__
34+
#define QALVL "[QA] "
35+
#endif
3336

3437
#define MAX_FILE_SIZE (5ul * 1024ul * 1024ul) /* 5MByte log file on disk */
3538
#define MAX_HEAP_SIZE (256 * 1024) /* 256k of memory */
@@ -604,6 +607,42 @@ void log_trace(const char *fmt,...)
604607
}
605608
} /* log_trace */
606609

610+
#ifdef __QATESTING__
611+
/* */
612+
/* @fn log_qa */
613+
/* @brief Print a message if the info logging level is enabled */
614+
/* @returns none */
615+
/* */
616+
void log_qa(const char *fmt,...)
617+
{
618+
get_log_format(logFormat, fmt, QALVL);
619+
620+
va_list args;
621+
va_start(args, fmt);
622+
size_t chars_to_write = vfprintf(stderr, logFormat, args);
623+
va_end(args);
624+
625+
if (config_loaded) {
626+
/* Write to the log buffer, too */
627+
size_t log_index = (log_tail - log_head); /* parasoft-suppress
628+
* MISRAC2012-DIR_4_1-i
629+
* "same array" */
630+
if (MAX_HEAP_SIZE <= (log_index + chars_to_write)) {
631+
632+
write_heap_to_disk();
633+
}
634+
get_log_format(logFormat, fmt, QALVL);
635+
va_list args;
636+
va_start(args, fmt);
637+
size_t chars_written = vsprintf(log_tail, logFormat, args);
638+
va_end(args);
639+
log_tail += chars_written;
640+
log_is_dirty = true;
641+
/* End write to the log buffer, too */
642+
}
643+
} /* log_warn */
644+
#endif
645+
607646
/* */
608647
/* @fn log_set_trace */
609648
/* @brief Turn on the trace & all lower logging levels */

logging.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ void log_info(const char* fmt, ...) __attribute__ ((format (printf, 1, 2)));
2222
void log_verbose(const char* fmt, ...) __attribute__ ((format (printf, 1, 2)));
2323
void log_debug(const char* fmt, ...) __attribute__ ((format (printf, 1, 2)));
2424
void log_trace(const char* fmt, ...) __attribute__ ((format (printf, 1, 2)));
25+
#ifdef __QATESTING__
26+
void log_qa(const char* fmt, ...) __attribute__ ((format (printf, 1, 2)));
27+
#endif
2528

2629
void log_set_trace(bool param);
2730
void log_set_debug(bool param);
@@ -48,4 +51,4 @@ void free_log_heap( void );
4851
#endif /* LOGGING_H_ */
4952
/******************************************************************************/
5053
/******************************* END OF FILE **********************************/
51-
/******************************************************************************/
54+
/******************************************************************************/

makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ openinstall: libagent.so
9595
sudo cp libagent.so /usr/lib
9696
sudo chmod 755 /usr/lib/libagent.so
9797

98+
qatesting: DEFINES += -D__OPEN_SSL__ -D__QATESTING__
99+
qatesting: ${OOBJ}
100+
${CC} ${CFLAGS} ${DEBUG_FLAGS} ${DEFINES} -o agent $^ ${OPENLIBS}
101+
98102
# The base build for a Raspberry Pi with a TPM installed
99103
rpi9670test: DEFINES += -D__OPEN_SSL__ -D__TPM__ -Wno-format
100104
rpi9670test: ${OOBJ}

management.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ int cms_job_manage(SessionJob_t * jobInfo, char *sessionToken,
383383
if (false == management_store_config_valid(manConf, &statusMessage)) {
384384
ManagementCompleteResp_t *manComp = NULL;
385385
send_management_job_complete(sessionToken, jobInfo->JobId,
386-
jobInfo->CompletionEndpoint, STAT_ERR, manConf->AuditId,
386+
jobInfo->CompletionEndpoint, STAT_ERR + 1, manConf->AuditId,
387387
statusMessage, &manComp);
388388
ManagementCompleteResp_free(manComp);
389389
returnable = 999;

session.c

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ static void add_custom_client_parameters(SessionRegisterReq_t * sessionReq) {
6565
* this: SessionRegisterReq_addNewClientParameter(sessionReq,
6666
* "devicetype", "linux");
6767
*/
68+
#ifdef __QATESTING__
69+
SessionRegisterReq_addNewClientParameter(sessionReq, "qatesting", "true");
70+
#endif
6871
return;
6972
} /* add_custom_client_parameters */
7073

@@ -135,17 +138,22 @@ static void update_config_from_session(SessionRegisterResp_t * sessionResp) {
135138
if (!ConfigData || !sessionResp) return;
136139
if (!ConfigData->EnrollOnStartup) return;
137140

138-
if (sessionResp->Session.Certificate) {
141+
if (sessionResp->Session.Certificate && sessionResp->Session.Certificate[0] != '\0') {
139142
log_info("%s::%s(%d) : Received Agent Certificate. Turning off EnrollOnStartup.", LOG_INF);
140143
isChanged = true;
141144
ConfigData->EnrollOnStartup = false;
145+
} else if (false == ConfigData->UseAgentCert) {
146+
log_info("%s::%s(%d) : Not using an Agent Certificate. Turning off EnrollOnStartup.", LOG_INF);
147+
isChanged = true;
148+
ConfigData->EnrollOnStartup = false;
149+
} else {
150+
log_info("%s::%s(%d) : Received no Agent Certificate. BUT, we should have received one.", LOG_INF);
142151
}
143152

144153
if (isChanged) {
145154
log_trace("%s::%s(%d) : Saving configuration to file system", LOG_INF);
146155
config_save();
147156
}
148-
return;
149157
} /* update_config_from_session */
150158

151159
/* */
@@ -290,8 +298,8 @@ static void prioritize_jobs(ScheduledJob_t **pJobList,
290298
schedule_job(pJobList, job_to_schedule, time(NULL));
291299
}
292300
}
293-
return;
294301
} /* prioritize_jobs */
302+
295303
/* */
296304
/* Add the capabilities allowed in this version of the agent by */
297305
/* capability GUID defined in Keyfactor */
@@ -376,7 +384,6 @@ static void set_registration_parameters(SessionRegisterReq_t * sessionReq) {
376384
/* Add any custom parameters for this customer */
377385
add_custom_client_parameters(sessionReq);
378386

379-
return;
380387
} /* set_registration_parameters */
381388

382389
/* */
@@ -808,34 +815,38 @@ static int re_register_agent(SessionInfo_t * session, ScheduledJob_t * *pJobList
808815
static bool do_first_registration_response(SessionRegisterResp_t * resp, char **status,
809816
enum AgentApiResultStatus *statusCode)
810817
{
811-
if (NULL == resp) {
812-
log_error("%s::%s(%d) : Error, response to parse is null", LOG_INF);
813-
return false;
814-
}
815-
bool bResult = false;
816-
bool bIdOk = false;
817-
log_trace("%s::%s(%d): Updating config from session", LOG_INF);
818-
bIdOk = update_agentid_from_session(resp);
819-
820-
if (ConfigData->UseAgentCert && bIdOk) {
821-
if (resp->Session.Certificate) {
822-
bResult = true;
823-
log_info("%s::%s(%d): Agent certificate recieved from platform. Saving Agent Specific Keypair and "
824-
"Agent Specific Cert.", LOG_INF);
825-
save_cert_key(ConfigData->AgentCert,
826-
ConfigData->AgentKey, ConfigData->AgentKeyPassword,
827-
resp->Session.Certificate, status, statusCode);
828-
update_config_from_session(resp);
829-
} else {
830-
/* The platform should have provided a certificate */
831-
log_error("%s::%s(%d): Certificate not found", LOG_INF);
832-
}
833-
} else {
834-
/* We don't need a certificate, but we do need an AgentId */
835-
if (bIdOk)
836-
bResult = true;
837-
}
838-
return bResult;
818+
if (NULL == resp) {
819+
log_error("%s::%s(%d) : Error, response to parse is null", LOG_INF);
820+
return false;
821+
}
822+
bool bResult = false;
823+
bool bIdOk = false;
824+
log_trace("%s::%s(%d): Updating config from session", LOG_INF);
825+
bIdOk = update_agentid_from_session(resp);
826+
827+
if (ConfigData->UseAgentCert && bIdOk) {
828+
if (resp->Session.Certificate) {
829+
bResult = true;
830+
log_info("%s::%s(%d): Agent certificate received from platform. Saving Agent Specific Keypair and "
831+
"Agent Specific Cert.", LOG_INF);
832+
save_cert_key(ConfigData->AgentCert,
833+
ConfigData->AgentKey, ConfigData->AgentKeyPassword,
834+
resp->Session.Certificate, status, statusCode);
835+
update_config_from_session(resp);
836+
} else {
837+
/* The platform should have provided a certificate */
838+
log_error("%s::%s(%d): Certificate not found", LOG_INF);
839+
}
840+
} else {
841+
/* We don't need a certificate, but we do need an AgentId */
842+
if (bIdOk)
843+
bResult = true;
844+
}
845+
846+
log_info("%s::%s(%d): First registration completed with %s",
847+
LOG_INF, bResult ? "success" : "failure");
848+
849+
return bResult;
839850
} /* do_first_registration_response */
840851

841852
/* */
@@ -930,7 +941,7 @@ int register_session(SessionInfo_t * session, ScheduledJob_t * *pJobList, uint64
930941
log_trace("%s::%s(%d) : Successfully set up /Session/Register data.", LOG_INF);
931942
}
932943
} else {
933-
log_trace("%s::%s(%d) : Skipping agent cert from configurtion", LOG_INF);
944+
log_trace("%s::%s(%d) : Configured to not use an Agent Certificate", LOG_INF);
934945
}
935946
} else {
936947
if (ConfigData->UseAgentCert) {
@@ -1062,12 +1073,15 @@ int register_session(SessionInfo_t * session, ScheduledJob_t * *pJobList, uint64
10621073
log_trace("%s::%s(%d) Performing second registration.", LOG_INF);
10631074
httpRes = do_second_registration(session, pJobList, agentVersion);
10641075
if (0 == httpRes) {
1065-
log_info("%s::%s(%d): Re-enrollment jobs set up successfully", LOG_INF);
1076+
log_info("%s::%s(%d): Re-enrollment jobs set up successfully", LOG_INF);
1077+
ConfigData->EnrollOnStartup = false;
1078+
config_save();
10661079
} else {
1067-
/* Session failed, so we need to re-register the agent */
1068-
/* on the next trigger */
1069-
ConfigData->EnrollOnStartup = true;
1070-
log_warn("%s::%s(%d) : Re-registering agent as second registration failed", LOG_INF);
1080+
/* Session failed, so we need to re-register the agent */
1081+
/* on the next trigger */
1082+
ConfigData->EnrollOnStartup = true;
1083+
log_warn("%s::%s(%d) : Re-registering agent as second registration failed", LOG_INF);
1084+
config_save();
10711085
}
10721086
}
10731087

0 commit comments

Comments
 (0)