-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold_dashboard.R
278 lines (246 loc) · 10.3 KB
/
old_dashboard.R
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
library(here)
here::i_am("lib.R")
source(here::here("lib.R"))
source(here::here("env.R"))
library(shiny)
library(shinydashboard)
library(shiny.fluent)
library(DT)
library(elastic)
library(RSQLite)
library(dplyr)
library(jsonlite)
library(roperators)
library(log4r)
library(tidyr)
library(kableExtra)
library(knitr)
options(shiny.host = shiny_host)
options(shiny.port = 8180)
# open connection with elasticsearch
con_elasticsearch <- connect(host = host_elasticsearch, user = user_elasticsearch, pwd = password_elasticsearch, port = port_elasticsearch, transport_schema = "http")
# open connection with sqlite
con_sqlite <- dbConnect(RSQLite::SQLite(), db_path)
# import ssl data from elasticsearch
ssl_data <- fromJSON(Search(con_elasticsearch, index = "ssl", size = 10000, raw = TRUE))$hits$hits$"_source" %>%
mutate(ipv4 = as.character(ipv4)) %>%
mutate(validFrom = as.Date(validFrom), validTo = as.Date(validTo)) %>%
dplyr::rename(ip = ipv4, date_debut = validFrom, date_fin = validTo)
# clean ssl data (hostname, ip, date_debut et date_fin)
ssl_specific <- ssl_data %>%
select(hostname, ip, date_debut, date_fin) %>%
arrange(hostname)
# table with all ssl data
ssl_all <- ssl_data
# selection of columns from ssl to display (sans ip et hostname car rajoutes plus tard)
column_default <- c("date_debut", "date_fin")
column_choices <- names(ssl_all)
column_choices <- column_choices[column_choices != "ip" & column_choices != "hostname"]
# notification
text_notification <- "..."
# necessaire si filtre dans menu sinon erreur
convertMenuItem <- function(mi, tabName) {
mi$children[[1]]$attribs["data-toggle"] <- "tab"
mi$children[[1]]$attribs["data-value"] <- tabName
mi
}
# title + notifications
header <- dashboardHeader(title = "Certificats SSL", dropdownMenuOutput("notifOutput"))
# sidebar (column selection)
sidebar <- dashboardSidebar(
collapsed = TRUE,
sidebarMenu(
convertMenuItem(
menuItem("Choix des colonnes",
tabName = "table",
icon = icon("list"),
checkboxGroupInput("columns_current", label = NULL, choices = column_choices, selected = column_default)
),
tabName = "table"
)
)
)
# body (filters (expired cert, period, resp, hostname and cert without resp) + tables (ssl, resp with roles and info cert))
body <- dashboardBody(
tabItems(
tabItem(
tabName = "table",
fluidPage(
fluidRow(
column(
width = 3,
box(
width = NULL,
h4(strong("Choix des filtres :"), style = "text-align: center;"),
checkboxInput("expired_filter", "Afficher les certificats échus ?", TRUE),
hr(style = "border-color: black;"),
checkboxInput("periode_filter", "Filtrer selon la période ?", FALSE),
conditionalPanel(
condition = "input.periode_filter == true", dateRangeInput("date_fin_plage", label = "Période comprenant la date d'échéance :", start = Sys.Date(), end = Sys.Date(), separator = " à ", format = "yyyy-mm-dd")
),
hr(style = "border-color: black;"),
checkboxInput("resp_filter", "Filtrer selon le responsable ?", FALSE),
conditionalPanel(
condition = "input.resp_filter == true", textInput("sciper", "Sciper d'un responsable :", value = "")
),
hr(style = "border-color: black;"),
checkboxInput("hostname_filter", "Filtrer selon le hostname ?", FALSE),
conditionalPanel(
condition = "input.hostname_filter == true", textInput("hostname", "Hostname d'un certificat :", value = "")
),
hr(style = "border-color: black; border-width: 3px;"),
checkboxInput("no_resp_filter", "Afficher uniquement les certificats sans responsable ?", FALSE)
)
),
column(
width = 9,
h4(strong("Affichage des échéances des certificats :"), style = "text-align: center;"),
DTOutput("df_all")
)
),
fluidRow(
hr(style = "border-color: black;"),
conditionalPanel(
condition = "input.df_all_rows_selected.length > 0", h4(strong("Affichage des responsables du certificat sélectionné :"), style = "text-align: center;")
),
DTOutput("df_resp")
)
)
)
)
)
# user interface
ui <- dashboardPage(
skin = "red",
header,
sidebar,
body
)
# server
server <- function(input, output, session) {
output$notifOutput <- renderMenu({
notif <- notificationItem(text_notification, icon = icon("warning"))
dropdownMenu(type = "notifications", notif)
})
# function to filter ssl data
filtered_data <- reactive({
data_filtred <- ssl_all
# time
date_fin_min <- input$date_fin_plage[1]
date_fin_max <- input$date_fin_plage[2]
if (!input$expired_filter) {
data_filtred <- data_filtred %>% filter(date_fin >= Sys.Date())
}
if (input$periode_filter && date_fin_min <= date_fin_max) {
data_filtred <- data_filtred %>% filter(date_fin >= date_fin_min & date_fin <= date_fin_max)
}
# sciper
if (input$resp_filter) {
sciper <- input$sciper
if (grepl("^[0-9]*$", sciper) && sciper != "") {
sciper <- as.integer(sciper)
ips <- dbGetQuery(con_sqlite, sprintf("SELECT User.id_user, User.sciper, Server.id_ip, Server.ip FROM User LEFT JOIN Server_User ON User.id_user = Server_User.id_user LEFT JOIN Server ON Server_User.id_ip = Server.id_ip WHERE sciper = %s;", sciper))
data_filtred <- data_filtred %>% filter(ip %in% ips$ip)
}
}
# hostname
if (input$hostname_filter) {
hn <- input$hostname
if (hn != "") {
data_filtred <- data_filtred %>% filter(hostname == hn)
}
}
# filter to control cert without resp
if (input$no_resp_filter) {
ips_cmdb <- dbGetQuery(con_sqlite, "SELECT Server.ip FROM Server")
data_filtred <- ssl_all %>% filter(ip %ni% ips_cmdb$ip)
}
# choice of columns
data <- data_filtred[, input$columns_current, drop = FALSE]
if (nrow(data) > 0) {
# add column with ip
data$ip <- data_filtred$ip
data <- data %>% select(ip, everything())
# add column with hostname
data$hostname <- data_filtred$hostname
data <- data %>% select(hostname, everything())
# add column to display pop up with certificate information
data$info <- '<i class=\"fa fa-info-circle\"></i>'
data <- data %>% select(info, everything())
} else {
data <- NULL
}
return(data)
})
# main table with ssl data and selected columns
output$df_all <- renderDT({
data_used <- filtered_data()
if (!is.null(data_used)) {
# hostname with link
data_used$hostname <- sprintf("<a href='https://%s' target='_blank'>%s</a>", data_used$hostname, data_used$hostname)
datatable(data_used, escape = FALSE, selection = "single", options = list(scrollX = TRUE, dom = "frtip", pageLength = 10), class = "stripe hover", rownames = FALSE)
} else {
datatable(data.frame(Message = "Aucun certificat ne correspond..."), selection = "single", options = list(dom = "rt", pageLength = 10), class = "stripe hover", rownames = FALSE)
}
})
# table of resp dependent on selected line in main table
output$df_resp <- renderDT({
req(input$df_all_rows_selected) # affichage uniquement si ligne selectionnee
selected_row <- input$df_all_rows_selected # index de la ligne selectionnee
selected_data <- filtered_data()[selected_row, , drop = FALSE]
ip <- selected_data$ip
info_user <- dbGetQuery(con_sqlite, sprintf("SELECT sciper, cn, email, rifs_flag, adminit_flag FROM Server LEFT JOIN Server_User ON Server.id_ip = Server_User.id_ip LEFT JOIN User ON Server_User.id_user = User.id_user WHERE Server.ip = '%s';", ip))
info_user <- info_user %>%
dplyr::rename(nom = cn, rifs = rifs_flag, adminit = adminit_flag) %>%
mutate(rifs = ifelse(rifs == 1, "x", ""), adminit = ifelse(adminit == 1, "x", "")) %>%
arrange(nom)
if (nrow(info_user) > 0) {
datatable(info_user, options = list(searching = TRUE, pageLength = 10), class = "stripe hover", rownames = FALSE)
} else {
datatable(data.frame(Message = "Aucun responsable assigné !"), selection = "single", options = list(dom = "rt", pageLength = 10), class = "stripe hover", rownames = FALSE)
}
})
# pop up when click on column "info" in main table to display cert info
observeEvent(input$df_all_cell_clicked, {
if (!is.null(input$df_all_cell_clicked$value)) {
if (input$df_all_cell_clicked$col == 0) {
selected_row <- filtered_data()[input$df_all_cell_clicked$row, , drop = FALSE]
cert_data <- ssl_all[selected_row$ip == ssl_all$ip & selected_row$hostname == ssl_all$hostname, ]
# subject name
output$subject_name <- renderTable({
cert_data$subject %>%
select(CN) %>%
dplyr::rename("Common Name" = CN)
})
# issuer name
output$issuer_name <- renderTable({
cert_data$issuer %>%
select(C, ST, L, O, CN) %>%
dplyr::rename("Country" = C, "State/Province" = ST, "Locality" = L, "Organization" = O, "Common Name" = CN)
})
# validity
output$validity <- renderUI({
cert_data %>%
select(date_debut, date_fin) %>%
dplyr::rename("Not Before" = date_debut, "Not After" = date_fin) %>%
kable(format = "html", row.names = FALSE) %>%
kable_styling() %>%
HTML()
})
# subject alt names
output$subject_alt_names <- renderTable({
san <- cert_data %>% select(san)
as.data.frame(lapply(san, function(col) {
if (is.list(col)) {
sapply(col, paste, collapse = ", ")
} else {
col
}
})) %>% dplyr::rename("DNS Name" = san)
})
showModal(modalDialog(title = "Informations du certificat", easyClose = TRUE, "Subject Name", tableOutput("subject_name"), tags$hr(style = "border-top: 1px solid #000;"), "Issuer Name", tableOutput("issuer_name"), tags$hr(style = "border-top: 1px solid #000;"), "Validity", uiOutput("validity"), tags$hr(style = "border-top: 1px solid #000;"), "Subject Alt Names", tableOutput("subject_alt_names"), footer = modalButton("Fermer")))
}
}
})
}
shinyApp(ui, server)