EncryptionControl.hpp 4.1 KB
Newer Older
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
/******************************************************************************
 *
 * This file is part of the Castor project.
 * See http://castor.web.cern.ch/castor
 *
 * Copyright (C) 2003  CERN
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *
 *
 * @author Castor Dev team, castor-dev@cern.ch
 *****************************************************************************/

#pragma once

#include <json-c/json.h>
27
#include <list>
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
#include <map>
#include <string>

#include "tapeserver/castor/tape/tapeserver/drive/DriveInterface.hpp"

namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {

/**
 * A class allowing the enabling and disabling of tape encryption via a script
 * provided by the operators. This script has the responsibility of providing
 * the encryption key to the tape drive.
 */
class EncryptionControl {
public:
  struct EncryptionStatus {
    bool on;
    std::string keyName; // encryption key identifier
    std::string key;     // encryption key
    std::string stdout;
  };

  enum SetTag {
    NO_SET_TAG,
    SET_TAG
  };

  /** @param scriptPath The path to the operator provided script for acquiring the key */
  EncryptionControl(const std::string & scriptPath);
  /**
   * Will call the encryption script provided by the operators to acquire the encryption key and then enable the
   * encryption if necessary.
   * @param m_drive The drive object on which the encryption is to be enabled.
   * @param vid VID for the tape cartridge we are about to mount.
   * @param st in order for the encryption script to set the VMGR tag or not.
   * @return {true, keyName, key, stdout} if the encryption has been set, {false, "", "", stdout} otherwise.
   */
  EncryptionStatus enable(castor::tape::tapeserver::drive::DriveInterface &m_drive, const std::string & vid, SetTag st);

  /**
   * Wrapper function to clear the encryption parameters from the drive - essentially meaning disabling the encryption.
   * @param m_drive The drive object on which the encryption is to be disabled.
   * @return true if encryption parameters cleared, false otherwise.
   */
  bool disable(castor::tape::tapeserver::drive::DriveInterface &m_drive);
private:
  std::string m_path; // Path to the key management script file
  /**
   * Parse the JSON output of the key management script and translate information into Encryption Status struct.
   * Expected to find keys key_id, encryption_key, message and the respective values as JSON strings.
   * @param input The JSON input to parse
   * @return {true, keyName, key, stdout} if the encryption has been set, {false, "", "", stdout} otherwise.
   */
  EncryptionStatus parse_json_script_output(std::string input);

  /**
   * Flatten all levels of a JSON object into a map of strings. When parsing nested JSON objects, it uses the key
   * of the object as prefix.
   * @param prefix Prefix for the keys of the map
   * @param jobj The JSON-C object which is flattened
   * @return A map of strings to strings representing the flattened hierarchy of the JSON input.
   */
  std::map<std::string, std::string> flatten_json_object_to_map(std::string prefix, json_object *jobj);

  /**
   * Flattens the arguments list of strings to a single string.
   * @param args The arguments list
   * @param delimiter The delimiter between the different arguments
   * @return A string representation of the arguments passed.
   */
  std::string argsToString(std::list<std::string> args, std::string delimiter);
};

}}}} // namespace castor::tape::tapeserver::daemon