#!/usr/bin/env bash

# Prowler - the handy cloud security tool (copyright 2020) by Toni de la Fuente
#
# 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 the
# specific language governing permissions and limitations under the License.



addHtmlHeader() {
  if [[ $PROFILE == "" ]];then
    PROFILE="ENV"
  fi
  if [[ -z $HTML_REPORT_INIT ]]; then 
  cat <<EOF
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <!-- Required meta tags -->
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <style>
	.read-more {color:#00f;}
	.bg-success-custom {background-color: #70dc88 !important;}
  </style>
  <!-- Bootstrap CSS -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
  <!-- https://datatables.net/download/index with jQuery, DataTables, Buttons, SearchPanes, and Select //-->
  <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/jqc-1.12.4/dt-1.10.25/b-1.7.1/sp-1.3.0/sl-1.3.3/datatables.min.css"/>
  <link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous"/>
  <style>
    .show-read-more .more-text{
        display: none;
    }
  </style>
  <title>Prowler - AWS Security Assessments</title>
</head>
<body>
  <nav class="navbar navbar-expand-xl sticky-top navbar-dark bg-dark">
      <a class="navbar-brand" href="#">Prowler - Security Assessments in AWS</a>
  </nav>
  <div class="container-fluid">
    <div class="row mt-3">
      <div class="col-md-4">
        <div class="card">
          <div class="card-header">
            Report Information:
          </div>
          <ul class="list-group list-group-flush">
            <li class="list-group-item">
              <div class="row">
                <div class="col-md-auto">
                  <b>Version:</b> $PROWLER_VERSION
                </div>
              </div>
            </li>
            <li class="list-group-item">
              <b>Parameters used:</b> $PROWLER_PARAMETERS
            </li>
            <li class="list-group-item">
              <b>Date:</b> $TIMESTAMP
            </li>
            <li class="list-group-item text-center">
              <a href="$HTML_LOGO_URL"><img src="$HTML_LOGO_IMG"
                  alt="prowler-logo"></a>
            </li>
          </ul>
        </div>
      </div>

      <div class="col-md-4">
        <div class="card">
          <div class="card-header">
            Assessment Summary:
          </div>
          <ul class="list-group list-group-flush">
            <li class="list-group-item">
              <b>AWS Account:</b> $ACCOUNT_NUM
            </li>
            <li class="list-group-item">
              <b>AWS-CLI Profile:</b> $PROFILE
            </li>
            <li class="list-group-item">
              <b>API Region:</b> $REGION
            </li>
            <li class="list-group-item">
              <b>User Id:</b> $USER_ID
            </li>
            <li class="list-group-item">
              <b>Caller Identity ARN:</b> $CALLER_ARN
            </li>
          </ul>
        </div>
      </div>
      <div class="col-md-4">
        <div class="card">
          <div class="card-header">
            Scoring Information:
          </div>
          <ul class="list-group list-group-flush">
            <li class="list-group-item">
              <b>Prowler Score:</b> PROWLER_SCORE%
            </li>
            <li class="list-group-item">
              <b>Total Resources:</b> TOTAL_RESOURCES
            </li>
            <li class="list-group-item">
              <b>Passed:</b> PASS_COUNTER
            </li>
            <li class="list-group-item">
              <b>Failed:</b> FAIL_COUNTER
            </li>
            <li class="list-group-item">
              <b>Total Checks Executed:</b> CHECKS_COUNTER
            </li>
          </ul>
        </div>
      </div>
    </div>
    <div class="row mt-3">
      <div class="col-md-12">
        <table class="table compact stripe row-border ordering" id="findingsTable" data-order='[[ 5, "asc" ]]' data-page-length='100'>
          <thead class="thead-light">
            <tr>
              <th scope="col">Result</th>
              <th scope="col">Severity</th>
              <th scope="col">AccountID</th>
              <th scope="col">Region</th>
              <th scope="col">Compliance</th>
              <th scope="col">Service</th>
              <th scope="col">CheckID</th>
              <th style="width:20%" scope="col">Check Title</th>
              <th style="width:20%" scope="col">Check Output</th>
              <th scope="col">CIS Level</th>
              <th scope="col">CAF Epic</th>
              <th scope="col">Risk</th>
              <th scope="col">Remediation</th>
              <th scope="col">Docs</th>
              <th scope="col">Resource ID</th>
            </tr>
          </thead>
          <tbody>
EOF

fi 
}

addHtmlFooter() {
  cat <<EOF

</tbody>
        </table>
      </div>
    </div>
  </div>
  </div>
  <!-- Table search and paginator -->
  <!-- Optional JavaScript -->
  <!-- jQuery first, then Popper.js, then Bootstrap JS -->
  <script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.bundle.min.js" integrity="sha384-1CmrxMRARb6aLqgBO7yyAxTOQE2AKb9GfXnEo760AUcUmFx3ibVJJAzGytlQcNXd" crossorigin="anonymous"></script>
  <!-- https://datatables.net/download/index with jQuery, DataTables, Buttons, SearchPanes, and Select //-->
  <script type="text/javascript" src="https://cdn.datatables.net/v/dt/jqc-1.12.4/dt-1.10.25/b-1.7.1/sp-1.3.0/sl-1.3.3/datatables.min.js"></script>

  <script>
    \$(document).ready(function(){

      // Initialise the table with 50 rows, and some search/filtering panes
      \$('#findingsTable').DataTable( {
        lengthMenu: [ [50, 100, -1], [50, 100, "All"] ],
        searchPanes: {
            cascadePanes: true,
            viewTotal: true
        },
        dom: 'Plfrtip',
        columnDefs: [
          {
              searchPanes: {
                  show: false
              },
              // Hide Compliance, Check ID (in favour of Check Title), CAF Epic, Risk, Remediation, Link
              targets: [4, 6, 9, 10, 11, 12]
          }
        ]
      });

      var maxLength = 30;
      \$(".show-read-more").each(function(){
        var myStr = \$(this).text();
        if(\$.trim(myStr).length > maxLength){
          var newStr = myStr.substring(0, maxLength);
          var removedStr = myStr.substring(maxLength, \$.trim(myStr).length);
          \$(this).empty().html(newStr);
          \$(this).append(' <a href="javascript:void(0);" class="read-more">read more...</a>');
          \$(this).append('<span class="more-text">' + removedStr + '</span>');
        }
      });
      \$(".read-more").click(function(){
        \$(this).siblings(".more-text").contents().unwrap();
        \$(this).remove();
      });
    });
    </script>

</body>
</html>
EOF

unset HTML_REPORT_INIT
}
