-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfetchlogs.c
More file actions
253 lines (212 loc) · 8.86 KB
/
fetchlogs.c
File metadata and controls
253 lines (212 loc) · 8.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/******************************************************************************/
/* Copyright 2021 Keyfactor */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain a */
/* copy of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless */
/* required by applicable law or agreed to in writing, software distributed */
/* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES */
/* OR CONDITIONS OF ANY KIND, either express or implied. See the License for */
/* thespecific language governing permissions and limitations under the */
/* License. */
/******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fetchlogs.h"
#include "logging.h"
#include "httpclient.h"
/******************************************************************************/
/***************************** LOCAL DEFINES *********************************/
/******************************************************************************/
/******************************************************************************/
/************************ LOCAL GLOBAL STRUCTURES *****************************/
/******************************************************************************/
/******************************************************************************/
/************************** LOCAL GLOBAL VARIABLES ****************************/
/******************************************************************************/
/******************************************************************************/
/************************ LOCAL FUNCTION DEFINITIONS **************************/
/******************************************************************************/
static int get_fetchlogs_config(const char* sessionToken, const char* jobId,
const char* endpoint, struct FetchLogsConfigResp** pConf)
{
char* url = NULL;
log_verbose("%s::%s(%d) : Sending config request: %s", LOG_INF, jobId);
struct CommonConfigReq* req = NULL;
req = CommonConfigReq_new();
if (!req) {
log_error("%s::%s(%d) : Error creating Common Config Request", LOG_INF);
return 999;
}
req->JobId = strdup(jobId);
req->SessionToken = strdup(sessionToken);
char* jsonReq = CommonConfigReq_toJson(req);
char* jsonResp = NULL;
url = config_build_url(endpoint, true);
int res = http_post_json(url, ConfigData->Username, ConfigData->Password,
ConfigData->TrustStore, ConfigData->AgentCert,
ConfigData->AgentKey, ConfigData->AgentKeyPassword,
jsonReq, &jsonResp, ConfigData->httpRetries,
ConfigData->retryInterval);
if(res == 0) {
*pConf = FetchLogsConfigResp_fromJson(jsonResp);
} else {
log_error("%s::%s(%d) : Config retrieval failed with error code %d", LOG_INF, res);
}
if (jsonReq) free(jsonReq);
if (jsonResp) free(jsonResp);
if (req) CommonConfigReq_free(req);
if (url) free(url);
return res;
}
static int get_logs(char* logFilePath, int maxCharactersToRead, char** log)
{
FILE* pLog;
char* logContent = NULL;
if((pLog = fopen(logFilePath, "r")) == NULL) {
log_error("%s::%s(%d) : Failed to open log file.", LOG_INF);
return 1;
}
if(fseek(pLog, 0, SEEK_END) != 0) {
log_error("%s::%s(%d) : End of file not found.", LOG_INF);
goto fail;
}
long int fileSize = ftell(pLog);
if(fseek(pLog, -1 * maxCharactersToRead, SEEK_END) != 0) {
rewind(pLog);
}
if(fileSize - ftell(pLog) == maxCharactersToRead) {
while(true) {
if ((char)fgetc(pLog) == '\n') {
(void)fseek(pLog, 1, SEEK_CUR);
break;
} else if(fgetc(pLog) == EOF) {
if(fseek(pLog, -1 * maxCharactersToRead, SEEK_END) != 0) {
(void)rewind(pLog);
}
break;
} else {
(void)fseek(pLog, 1, SEEK_CUR);
}
}
}
logContent = calloc((size_t)maxCharactersToRead, sizeof(*logContent));
if (!logContent) {
log_error("%s::%s(%d) : Out of memory", LOG_INF);
goto fail;
}
size_t readCount = fread(logContent, sizeof(char),(size_t)maxCharactersToRead, pLog);
if ((readCount < (size_t)maxCharactersToRead) && (0 != ferror(pLog))) {
}
*log = logContent;
if (pLog) (void)fclose(pLog);
return 0;
fail:
if (pLog) (void)fclose(pLog);
return 1;
}
static int send_fetchlogs_job_complete(const char* sessionToken,
const char* jobId, const char* endpoint, int jobStatus, long auditId,
const char* message, const char* log, struct CommonCompleteResp** pComp)
{
char* url = NULL;
log_verbose("%s::%s(%d) : Sending complete request: %ld for session: %s", LOG_INF, auditId, sessionToken);
struct FetchLogsCompleteReq* req = FetchLogsCompleteReq_new();
if (!req) {
log_error("%s::%s(%d) : Error creating Fetch Logs Complete Request Structure", LOG_INF);
return 999;
}
if (sessionToken) {
req->SessionToken = strdup(sessionToken);
} else {
req->SessionToken = strdup("Error no session token");
}
if (jobId) {
req->JobId = strdup(jobId);
} else {
req->JobId = strdup("Error no JobId");
}
req->Status = jobStatus;
req->AuditId = auditId;
if (message) {
req->Message = strdup(message);
} else {
req->Message = strdup("");
}
if (log) {
req->Log = strdup(log);
} else {
req->Log = strdup("Log retrieval error!");
}
char* jsonReq = FetchLogsCompleteReq_toJson(req);
char* jsonResp = NULL;
url = config_build_url(endpoint, true);
int res = http_post_json(url, ConfigData->Username, ConfigData->Password,
ConfigData->TrustStore, ConfigData->AgentCert, ConfigData->AgentKey,
ConfigData->AgentKeyPassword, jsonReq, &jsonResp,
ConfigData->httpRetries, ConfigData->retryInterval);
if(res == 0) {
*pComp = CommonCompleteResp_fromJson(jsonResp);
} else {
log_error("%s::%s(%d) : Job completion failed with error code %d", LOG_INF, res);
}
if (jsonReq) free(jsonReq);
if (jsonResp) free(jsonResp);
if (url) free(url);
if (req) FetchLogsCompleteReq_free(req);
return res;
}
/******************************************************************************/
/*********************** GLOBAL FUNCTION DEFINITIONS **************************/
/******************************************************************************/
int cms_job_fetchLogs(struct SessionJob* jobInfo, char* sessionToken)
{
int res = 0;
int returnable = 0;
struct FetchLogsConfigResp* fetchLogsConf = NULL;
char* statusMessage = strdup("");
enum AgentApiResultStatus status = STAT_UNK;
log_info("%s::%s(%d) : Starting fetch logs job %s",
LOG_INF, jobInfo->JobId);
res = get_fetchlogs_config(sessionToken, jobInfo->JobId,
jobInfo->ConfigurationEndpoint, &fetchLogsConf);
if(res == 0 && fetchLogsConf && AgentApiResult_log(fetchLogsConf->Result,
&statusMessage, &status))
{
if(fetchLogsConf->JobCancelled)
{
returnable = 1;
log_info("%s::%s(%d) : Job has been cancelled and will not be run",
LOG_INF);
}
else
{
log_verbose("%s::%s(%d) : Audit Id: %ld", LOG_INF,
fetchLogsConf->AuditId);
char* log = NULL;
struct CommonCompleteResp* compResponse = NULL;
/* pull the last 4000 characters from the log file. */
/* (trim beginning to the first \n if necessary). */
status = STAT_SUCCESS;
if( 0 != get_logs(ConfigData->LogFile,
fetchLogsConf->MaxCharactersToReturn, &log) )
{
status = STAT_ERR;
}
/* Complete job. */
res = send_fetchlogs_job_complete(sessionToken, jobInfo->JobId,
jobInfo->CompletionEndpoint, (status+1),
fetchLogsConf->AuditId, statusMessage, log, &compResponse);
log_verbose("%s::%s : %s", __FILE__, __FUNCTION__, log);
free(log);
CommonCompleteResp_free(compResponse);
}
}
/* free memory */
FetchLogsConfigResp_free(fetchLogsConf);
free(statusMessage);
return returnable;
}
/******************************************************************************/
/******************************* END OF FILE **********************************/
/******************************************************************************/