Statistics
| Branch: | Tag: | Revision:

root / lib / confd / server.py @ 6daf26a0

History | View | Annotate | Download (4.8 kB)

1
#!/usr/bin/python
2
#
3

    
4
# Copyright (C) 2009, Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

    
21

    
22
"""Ganeti configuration daemon server library.
23

24
Ganeti-confd is a daemon to query master candidates for configuration values.
25
It uses UDP+HMAC for authentication with a global cluster key.
26

27
"""
28

    
29
import logging
30
import time
31

    
32
from ganeti import constants
33
from ganeti import objects
34
from ganeti import errors
35
from ganeti import utils
36
from ganeti import serializer
37

    
38
from ganeti.confd import querylib
39

    
40

    
41
class ConfdProcessor(object):
42
  """A processor for confd requests.
43

44
  """
45
  DISPATCH_TABLE = {
46
      constants.CONFD_REQ_PING: querylib.PingQuery,
47
      constants.CONFD_REQ_NODE_ROLE_BYNAME: querylib.NodeRoleQuery,
48
      constants.CONFD_REQ_NODE_PIP_BY_INSTANCE_IP: querylib.ConfdQuery,
49
  }
50

    
51
  def __init__(self, reader):
52
    """Constructor for ConfdProcessor
53

54
    @type reader: L{ssconf.SimpleConfigReader}
55
    @param reader: ConfigReader to use to access the config
56

57
    """
58
    self.reader = reader
59
    self.hmac_key = utils.ReadFile(constants.HMAC_CLUSTER_KEY)
60
    assert \
61
      not constants.CONFD_REQS.symmetric_difference(self.DISPATCH_TABLE), \
62
      "DISPATCH_TABLE is unaligned with CONFD_REQS"
63

    
64
  def ExecQuery(self, payload_in, ip, port):
65
    """Process a single UDP request from a client.
66

67
    @type payload_in: string
68
    @param payload_in: request raw data
69
    @type ip: string
70
    @param ip: source ip address
71
    @param port: integer
72
    @type port: source port
73

74
    """
75
    try:
76
      request = self.ExtractRequest(payload_in)
77
      reply, rsalt = self.ProcessRequest(request)
78
      payload_out = self.PackReply(reply, rsalt)
79
      return payload_out
80
    except errors.ConfdRequestError, err:
81
      logging.info('Ignoring broken query from %s:%d: %s' % (ip, port, err))
82
      return None
83

    
84
  def ExtractRequest(self, payload):
85
    """Extracts a ConfdRequest object from a serialized hmac signed string.
86

87
    This functions also performs signature/timestamp validation.
88

89
    """
90
    current_time = time.time()
91
    logging.debug("Extracting request with size: %d" % (len(payload)))
92
    try:
93
      (message, salt) = serializer.LoadSigned(payload, self.hmac_key)
94
    except errors.SignatureError, err:
95
      msg = "invalid signature: %s" % err
96
      raise errors.ConfdRequestError(msg)
97
    try:
98
      message_timestamp = int(salt)
99
    except (ValueError, TypeError):
100
      msg = "non-integer timestamp: %s" % salt
101
      raise errors.ConfdRequestError(msg)
102

    
103
    skew = abs(current_time - message_timestamp)
104
    if skew > constants.CONFD_MAX_CLOCK_SKEW:
105
      msg = "outside time range (skew: %d)" % skew
106
      raise errors.ConfdRequestError(msg)
107

    
108
    try:
109
      request = objects.ConfdRequest.FromDict(message)
110
    except AttributeError, err:
111
      raise errors.ConfdRequestError('%s' % err)
112

    
113
    return request
114

    
115
  def ProcessRequest(self, request):
116
    """Process one ConfdRequest request, and produce an answer
117

118
    @type request: L{objects.ConfdRequest}
119
    @rtype: (L{objects.ConfdReply}, string)
120
    @return: tuple of reply and salt to add to the signature
121

122
    """
123
    logging.debug("Processing request: %s" % request)
124
    if request.protocol != constants.CONFD_PROTOCOL_VERSION:
125
      msg = "wrong protocol version %d" % request.protocol
126
      raise errors.ConfdRequestError(msg)
127

    
128
    if request.type not in constants.CONFD_REQS:
129
      msg = "wrong request type %d" % request.type
130
      raise errors.ConfdRequestError(msg)
131

    
132
    rsalt = request.rsalt
133
    if not rsalt:
134
      msg = "missing requested salt"
135
      raise errors.ConfdRequestError(msg)
136

    
137
    query_object = self.DISPATCH_TABLE[request.type](self.reader)
138
    status, answer = query_object.Exec(request.query)
139
    reply = objects.ConfdReply(
140
              protocol=constants.CONFD_PROTOCOL_VERSION,
141
              status=status,
142
              answer=answer,
143
              serial=self.reader.GetConfigSerialNo(),
144
              )
145

    
146
    logging.debug("Sending reply: %s" % reply)
147

    
148
    return (reply, rsalt)
149

    
150
  def PackReply(self, reply, rsalt):
151
    """Serialize and sign the given reply, with salt rsalt
152

153
    @type reply: L{objects.ConfdReply}
154
    @type rsalt: string
155

156
    """
157
    return serializer.DumpSigned(reply.ToDict(), self.hmac_key, rsalt)
158