# Copyright 2019 The inspec-gcp-cis-benchmark Authors
#
# 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
#
#     https://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.

title 'Ensure That Cloud SQL Database Instances Do Not Have Public IPs'

gcp_project_id = input('gcp_project_id')
cis_version = input('cis_version')
cis_url = input('cis_url')
control_id = '6.6'
control_abbrev = 'db'

sql_cache = CloudSQLCache(project: gcp_project_id)

control "cis-gcp-#{control_id}-#{control_abbrev}" do
  impact 'high'

  title "[#{control_abbrev.upcase}] Ensure That Cloud SQL Database Instances Do Not Have Public IPs"

  desc 'It is recommended to configure Second Generation Sql instance to use private IPs instead of public IPs.'
  desc 'rationale', "To lower the organization's attack surface, Cloud SQL databases should not have public IPs.
        Private IPs provide improved network security and lower latency for your application."

  tag cis_scored: true
  tag cis_level: 2
  tag cis_gcp: control_id.to_s
  tag cis_version: cis_version.to_s
  tag project: gcp_project_id.to_s
  tag nist: ['SC-1']

  ref 'CIS Benchmark', url: cis_url.to_s
  ref 'GCP Docs', url: 'https://cloud.google.com/sql/docs/mysql/configure-private-ip'
  ref 'GCP Docs', url: 'https://cloud.google.com/sql/docs/mysql/private-ip'
  ref 'GCP Docs', url: 'https://cloud.google.com/resource-manager/docs/organization-policy/org-policy-constraints'
  ref 'GCP Docs', url: 'https://console.cloud.google.com/iam-admin/orgpolicies/sql-restrictPublicIp'

  if sql_cache.instance_names.empty?
    impact 'none'
    describe "[#{gcp_project_id}] does not have CloudSQL instances. This test is Not Applicable." do
      skip "[#{gcp_project_id}] does not have CloudSQL instances."
    end
  else
    sql_cache.instance_names.each do |db|
      sql_cache.instance_objects[db].ip_addresses.each do |ip_address|
        describe "[#{gcp_project_id}] CloudSQL #{db}" do
          subject { ip_address }
          its('type') { should_not include('PRIMARY') }
        end
      end
    end
  end
end
