#
# Cookbook Name:: openvpn
# Recipe:: revokes
#
# Copyright 2014, CREATIONLINE,INC.
#
# 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.
#

search("revokes", "*:*") do |u|
  execute "revoke-openvpn-#{u['id']}" do
    command "#{node["openvpn"]['revoke_full']} #{u['id']} && touch #{node["openvpn"]["key_dir"]}/#{u['id']}.crl"
    cwd node["openvpn"]["key_dir"]
    environment(
      'OPENSSL' => 'openssl',
      'EASY_RSA' => '/etc/openvpn/easy-rsa',
      'KEY_CONFIG' => '/etc/openvpn/easy-rsa/openssl.cnf',
      'KEY_DIR' => node["openvpn"]["key_dir"],
      'CA_EXPIRE' => node["openvpn"]["key"]["ca_expire"].to_s,
      'KEY_EXPIRE' => node["openvpn"]["key"]["expire"].to_s,
      'KEY_SIZE' => node["openvpn"]["key"]["size"].to_s,
      'KEY_COUNTRY' => node["openvpn"]["key"]["country"],
      'KEY_PROVINCE' => node["openvpn"]["key"]["province"],
      'KEY_CITY' => node["openvpn"]["key"]["city"],
      'KEY_ORG' => node["openvpn"]["key"]["org"],
      'KEY_EMAIL' => node["openvpn"]["key"]["email"]
    )
    not_if { ::File.exists?("#{node["openvpn"]["key_dir"]}/#{u['id']}.crl") }
  end
end

execute "move-crl-pem" do
  command "mv #{node["openvpn"]["key_dir"]}/crl.pem /etc/openvpn/"
  not_if { ::File.symlink?( "#{node["openvpn"]["key_dir"]}/crl.pem" ) }
end

link "#{node["openvpn"]["key_dir"]}/crl.pem" do
  to "/etc/openvpn/crl.pem"
  not_if { ::File.symlink?( "#{node["openvpn"]["key_dir"]}/crl.pem" ) }
end
