#!/usr/bin/env bash

#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#

# validate the config.yaml

. ./t/cli/common.sh

# test proxy_ssl_trusted_certificate success
git checkout conf/config.yaml

exit_if_not_customed_nginx

echo '
apisix:
  ssl:
    ssl_trusted_certificate: t/certs/apisix.crt
nginx_config:
  http_configuration_snippet: |
    server {
        listen 1983 ssl;
        server_name test.com;
        ssl_certificate             ../t/certs/apisix.crt;
        ssl_certificate_key         ../t/certs/apisix.key;
        location /hello {
            return 200 "hello world";
        }
    }
  http_server_configuration_snippet: |
    proxy_ssl_verify on;
' > conf/config.yaml

rm logs/error.log || true
make init
make run
sleep 0.1

admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g')
curl -k -i http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X PUT -d '
{
    "uri": "/hello",
    "upstream": {
        "pass_host": "rewrite",
        "nodes": {
            "127.0.0.1:1983": 1
        },
        "scheme": "https",
        "hash_on": "vars",
        "upstream_host": "test.com",
        "type": "roundrobin",
        "tls": {
            "client_cert": "-----BEGIN CERTIFICATE-----\nMIIEojCCAwqgAwIBAgIJAK253pMhgCkxMA0GCSqGSIb3DQEBCwUAMFYxCzAJBgNV\nBAYTAkNOMRIwEAYDVQQIDAlHdWFuZ0RvbmcxDzANBgNVBAcMBlpodUhhaTEPMA0G\nA1UECgwGaXJlc3R5MREwDwYDVQQDDAh0ZXN0LmNvbTAgFw0xOTA2MjQyMjE4MDVa\nGA8yM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Q4ksqhESvjztuy58qk9aZHsQB8ZKRu8VSay40a/3ueX6bnd0hwsYy42aWJR1z+uie3yTWPuG2JZ7DjkgDduWdC+cxfvTVTG58E5luafy5j/t85UVoB2nr46VHlt/vg4M9G8/4F0d0Y6ThI4/XTfg6l1vq5ouzhQxd+SRwnuXieZy+4/2XKJnrV6t+JbNAvwdGR1V9VPLlnb+IqpvOCYyL1YLYSlNubb9HU0wxVPppGSpJLmi+njQzl71PBgMm6QV9j889wPUo387fRbJjXbSSVLon61xk/4dNvjsgfv9rF+/qEML0q4tXBJVOJ1iwKjn84Nk6vdHM3Hu8knp0hYFa4AECYKInSTVXajWAKFx4SOq8G8MA/0YlIN872LBjUm2GKs17wsJuWID+mSyVE5pV5gQ+r92YvPcC+yIvB8hTTaRclAP/KyJesDTA=="
        }
    }
}'

sleep 1

code=$(curl -v -k -i -m 20 -o /dev/null -s -w %{http_code} http://127.0.0.1:9080/hello)

if [ ! $code -eq 200 ]; then
    echo "failed: connection to upstream with mTLS failed"
    exit 1
fi

sleep 0.1

make stop

echo "passed: connection to upstream with mTLS success"

# test proxy_ssl_trusted_certificate and use incorrect ca cert
echo '
apisix:
  ssl:
    ssl_trusted_certificate: t/certs/apisix_ecc.crt
nginx_config:
  http_configuration_snippet: |
    server {
        listen 1983 ssl;
        server_name test.com;
        ssl_certificate             ../t/certs/apisix.crt;
        ssl_certificate_key         ../t/certs/apisix.key;
        location /hello {
            return 200 "hello world";
        }
    }
  http_server_configuration_snippet: |
    proxy_ssl_verify on;
' > conf/config.yaml

rm logs/error.log || true
make init
make run
sleep 0.1

admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g')
curl -k -i http://127.0.0.1:9180/apisix/admin/routes/1 -H "X-API-KEY: $admin_key" -X PUT -d '
{
    "uri": "/hello",
    "upstream": {
        "pass_host": "rewrite",
        "nodes": {
            "127.0.0.1:1983": 1
        },
        "scheme": "https",
        "hash_on": "vars",
        "upstream_host": "test.com",
        "type": "ro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P7iTcp4pi+qJHIWCN1ElDI0BVoZ+Xq9iLfKmjrjcxQ7EYGHfQDE52QaCQ3nMB7oiqncZ1Q5n/ICDHha9RkPP9V9vWiJIZwgOJtPfGzsGQ9AigH6po65IJyxmY5upuhg7DTmsLQnKC/fwjkBF9So/4cdZuqDbxGrDDOgpL7uvWXANRNMrqYoMFUG7M90QJHj7NgSL+B6mSNwa9ctTua7Estkoyvavda3Bl3qHQ0Hva5gjSg6elL6PQ4ksqhESvjztuy58qk9aZHsQB8ZKRu8VSay40a/3ueX6bnd0hwsYy42aWJR1z+uie3yTWPuG2JZ7DjkgDduWdC+cxfvTVTG58E5luafy5j/t85UVoB2nr46VHlt/vg4M9G8/4F0d0Y6ThI4/XTfg6l1vq5ouzhQxd+SRwnuXieZy+4/2XKJnrV6t+JbNAvwdGR1V9VPLlnb+IqpvOCYyL1YLYSlNubb9HU0wxVPppGSpJLmi+njQzl71PBgMm6QV9j889wPUo387fRbJjXbSSVLon61xk/4dNvjsgfv9rF+/qEML0q4tXBJVOJ1iwKjn84Nk6vdHM3Hu8knp0hYFa4AECYKInSTVXajWAKFx4SOq8G8MA/0YlIN872LBjUm2GKs17wsJuWID+mSyVE5pV5gQ+r92YvPcC+yIvB8hTTaRclAP/KyJesDTA=="
        }
    }
}'

sleep 0.1

code=$(curl -v -k -i -m 20 -o /dev/null -s -w %{http_code} http://127.0.0.1:9080/hello)

if [ ! $code -eq 502 ]; then
    echo "failed: should fail when proxy_ssl_verify is enabled and ssl_trusted_certificate is wrong ca cert"
    exit 1
fi

sleep 0.1

make stop

if ! grep -E 'self-signed certificate' logs/error.log; then
    echo "failed: should got 'self-signed certificate' when ssl_trusted_certificate is wrong ca cert"
    exit 1
fi

echo "passed: when proxy_ssl_verify is enabled and ssl_trusted_certificate is wrong ca cert, got 502"
