File: contents.py

package info (click to toggle)
python-pykmip 0.10.0-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,780 kB
  • sloc: python: 102,455; makefile: 33; sh: 12
file content (514 lines) | stat: -rw-r--r-- 16,080 bytes parent folder | download | duplicates (3)
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
27
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# Copyright (c) 2014 The Johns Hopkins University/Applied Physics Laboratory
# All Rights Reserved.
#
# 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.

import six

from kmip.core import enums
from kmip.core import objects
from kmip.core import utils

from kmip.core import primitives
from kmip.core.primitives import Struct
from kmip.core.primitives import Integer
from kmip.core.primitives import Enumeration
from kmip.core.primitives import Boolean
from kmip.core.primitives import TextString
from kmip.core.primitives import ByteString
from kmip.core.primitives import DateTime


class ProtocolVersion(primitives.Struct):
    """
    A struct representing a ProtocolVersion number.

    Attributes:
        major: The major protocol version number.
        minor: The minor protocol version number.
    """

    def __init__(self, major=None, minor=None):
        """
        Construct a ProtocolVersion struct.

        Args:
            major (int): The major protocol version number. Optional, defaults
                to None.
            minor (int): The minor protocol version number. Optional, defaults
                to None.
        """
        super(ProtocolVersion, self).__init__(enums.Tags.PROTOCOL_VERSION)

        self._major = None
        self._minor = None

        self.major = major
        self.minor = minor

    @property
    def major(self):
        if self._major:
            return self._major.value
        else:
            return None

    @major.setter
    def major(self, value):
        if value is None:
            self._major = None
        elif isinstance(value, six.integer_types):
            self._major = primitives.Integer(
                value=value,
                tag=enums.Tags.PROTOCOL_VERSION_MAJOR
            )
        else:
            raise TypeError(
                "Major protocol version number must be an integer."
            )

    @property
    def minor(self):
        if self._minor:
            return self._minor.value
        else:
            return None

    @minor.setter
    def minor(self, value):
        if value is None:
            self._minor = None
        elif isinstance(value, six.integer_types):
            self._minor = primitives.Integer(
                value=value,
                tag=enums.Tags.PROTOCOL_VERSION_MINOR
            )
        else:
            raise TypeError(
                "Minor protocol version number must be an integer."
            )

    def read(self, input_stream, kmip_version=enums.KMIPVersion.KMIP_1_0):
        """
        Read the data encoding the ProtocolVersion struct and decode it into
        its constituent parts.

        Args:
            input_stream (stream): A data stream containing encoded object
                data, supporting a read method; usually a BytearrayStream
                object.
            kmip_version (KMIPVersion): An enumeration defining the KMIP
                version with which the object will be decoded. Optional,
                defaults to KMIP 1.0.

        Raises:
            ValueError: Raised if either the major or minor protocol versions
                are missing from the encoding.
        """
        super(ProtocolVersion, self).read(
            input_stream,
            kmip_version=kmip_version
        )
        local_stream = utils.BytearrayStream(input_stream.read(self.length))

        if self.is_tag_next(enums.Tags.PROTOCOL_VERSION_MAJOR, local_stream):
            self._major = primitives.Integer(
                tag=enums.Tags.PROTOCOL_VERSION_MAJOR
            )
            self._major.read(local_stream, kmip_version=kmip_version)
        else:
            raise ValueError(
                "Invalid encoding missing the major protocol version number."
            )

        if self.is_tag_next(enums.Tags.PROTOCOL_VERSION_MINOR, local_stream):
            self._minor = primitives.Integer(
                tag=enums.Tags.PROTOCOL_VERSION_MINOR
            )
            self._minor.read(local_stream, kmip_version=kmip_version)
        else:
            raise ValueError(
                "Invalid encoding missing the minor protocol version number."
            )

        self.is_oversized(local_stream)

    def write(self, output_stream, kmip_version=enums.KMIPVersion.KMIP_1_0):
        """
        Write the data encoding the ProtocolVersion struct to a stream.

        Args:
            output_stream (stream): A data stream in which to encode object
                data, supporting a write method; usually a BytearrayStream
                object.
            kmip_version (KMIPVersion): An enumeration defining the KMIP
                version with which the object will be encoded. Optional,
                defaults to KMIP 1.0.

        Raises:
            ValueError: Raised if the data attribute is not defined.
        """
        local_stream = utils.BytearrayStream()

        if self._major:
            self._major.write(local_stream, kmip_version=kmip_version)
        else:
            raise ValueError(
                "Invalid struct missing the major protocol version number."
            )

        if self._minor:
            self._minor.write(local_stream, kmip_version=kmip_version)
        else:
            raise ValueError(
                "Invalid struct missing the minor protocol version number."
            )

        self.length = local_stream.length()
        super(ProtocolVersion, self).write(
            output_stream,
            kmip_version=kmip_version
        )
        output_stream.write(local_stream.buffer)

    def __eq__(self, other):
        if isinstance(other, ProtocolVersion):
            if self.major != other.major:
                return False
            elif self.minor != other.minor:
                return False
            else:
                return True
        else:
            return NotImplemented

    def __ne__(self, other):
        if isinstance(other, ProtocolVersion):
            return not (self == other)
        else:
            return NotImplemented

    def __lt__(self, other):
        if isinstance(other, ProtocolVersion):
            if self.major < other.major:
                return True
            elif self.major > other.major:
                return False
            elif self.minor < other.minor:
                return True
            else:
                return False
        else:
            return NotImplemented

    def __gt__(self, other):
        if isinstance(other, ProtocolVersion):
            if (self == other) or (self < other):
                return False
            else:
                return True
        else:
            return NotImplemented

    def __le__(self, other):
        if isinstance(other, ProtocolVersion):
            if (self == other) or (self < other):
                return True
            else:
                return False
        else:
            return NotImplemented

    def __ge__(self, other):
        if isinstance(other, ProtocolVersion):
            if (self == other) or (self > other):
                return True
            else:
                return False
        else:
            return NotImplemented

    def __repr__(self):
        args = ", ".join([
            "major={}".format(self.major),
            "minor={}".format(self.minor)
        ])
        return "ProtocolVersion({})".format(args)

    def __str__(self):
        return "{}.{}".format(self.major, self.minor)


def protocol_version_to_kmip_version(value):
    """
    Convert a ProtocolVersion struct to its KMIPVersion enumeration equivalent.

    Args:
        value (ProtocolVersion): A ProtocolVersion struct to be converted into
            a KMIPVersion enumeration.

    Returns:
        KMIPVersion: The enumeration equivalent of the struct. If the struct
            cannot be converted to a valid enumeration, None is returned.
    """
    if not isinstance(value, ProtocolVersion):
        return None

    if value.major == 1:
        if value.minor == 0:
            return enums.KMIPVersion.KMIP_1_0
        elif value.minor == 1:
            return enums.KMIPVersion.KMIP_1_1
        elif value.minor == 2:
            return enums.KMIPVersion.KMIP_1_2
        elif value.minor == 3:
            return enums.KMIPVersion.KMIP_1_3
        elif value.minor == 4:
            return enums.KMIPVersion.KMIP_1_4
        else:
            return None
    elif value.major == 2:
        if value.minor == 0:
            return enums.KMIPVersion.KMIP_2_0
        else:
            return None
    else:
        return None


# 6.2
class Operation(Enumeration):

    def __init__(self, value=None):
        super(Operation, self).__init__(
            enums.Operation, value, enums.Tags.OPERATION)


# 6.3
class MaximumResponseSize(Integer):
    def __init__(self, value=None):
        super(MaximumResponseSize, self).\
            __init__(value, enums.Tags.MAXIMUM_RESPONSE_SIZE)


# 6.4
class UniqueBatchItemID(ByteString):
    def __init__(self, value=None):
        super(UniqueBatchItemID, self)\
            .__init__(value, enums.Tags.UNIQUE_BATCH_ITEM_ID)


# 6.5
class TimeStamp(DateTime):
    def __init__(self, value=None):
        super(TimeStamp, self).__init__(value, enums.Tags.TIME_STAMP)


class Authentication(Struct):
    """
    A struct representing an Authentication bundle.

    Attributes:
        credentials: A list of Credential structs to be used for
            authentication.
    """

    def __init__(self, credentials=None):
        """
        Construct an Authentication struct.

        Args:
            credentials (list): A list of Credential structs to be used for
                authentication. Optional, defaults to None.
        """
        super(Authentication, self).__init__(enums.Tags.AUTHENTICATION)

        self._credentials = []
        self.credentials = credentials

    @property
    def credentials(self):
        return self._credentials

    @credentials.setter
    def credentials(self, value):
        if value is None:
            self._credentials = []
        elif isinstance(value, list):
            credentials = []
            for i in range(len(value)):
                credential = value[i]
                if not isinstance(credential, objects.Credential):
                    raise TypeError(
                        "Credentials must be a list of Credential structs. "
                        "Item {} has type: {}".format(i + 1, type(credential))
                    )
                credentials.append(credential)
            self._credentials = credentials
        else:
            raise TypeError(
                "Credentials must be a list of Credential structs."
            )

    def read(self, input_stream, kmip_version=enums.KMIPVersion.KMIP_1_0):
        """
        Read the data encoding the Authentication struct and decode it into
        its constituent parts.

        Args:
            input_stream (stream): A data stream containing encoded object
                data, supporting a read method; usually a BytearrayStream
                object.
            kmip_version (KMIPVersion): An enumeration defining the KMIP
                version with which the object will be decoded. Optional,
                defaults to KMIP 1.0.
        """
        super(Authentication, self).read(
            input_stream,
            kmip_version=kmip_version
        )
        local_stream = utils.BytearrayStream(input_stream.read(self.length))

        credentials = []
        while self.is_tag_next(enums.Tags.CREDENTIAL, local_stream):
            credential = objects.Credential()
            credential.read(local_stream, kmip_version=kmip_version)
            credentials.append(credential)
        if len(credentials) == 0:
            raise ValueError("Authentication encoding missing credentials.")
        self._credentials = credentials

        self.is_oversized(local_stream)

    def write(self, output_stream, kmip_version=enums.KMIPVersion.KMIP_1_0):
        """
        Write the data encoding the Authentication struct to a stream.

        Args:
            output_stream (stream): A data stream in which to encode object
                data, supporting a write method; usually a BytearrayStream
                object.
            kmip_version (KMIPVersion): An enumeration defining the KMIP
                version with which the object will be encoded. Optional,
                defaults to KMIP 1.0.
        """
        local_stream = utils.BytearrayStream()

        if len(self._credentials) == 0:
            raise ValueError("Authentication struct missing credentials.")
        for credential in self._credentials:
            credential.write(local_stream, kmip_version=kmip_version)

        self.length = local_stream.length()
        super(Authentication, self).write(
            output_stream,
            kmip_version=kmip_version
        )
        output_stream.write(local_stream.buffer)

    def __eq__(self, other):
        if isinstance(other, Authentication):
            if self.credentials != other.credentials:
                return False
            else:
                return True
        else:
            return NotImplemented

    def __ne__(self, other):
        if isinstance(other, Authentication):
            return not (self == other)
        else:
            return NotImplemented

    def __repr__(self):
        args = ", ".join([
            "credentials={}".format([x for x in self.credentials])
        ])
        return "Authentication({})".format(args)

    def __str__(self):
        credentials = ", ".join([str(x) for x in self.credentials])
        return "{'credentials': [" + credentials + "]}"


# 6.7
class AsynchronousIndicator(Boolean):
    def __init__(self, value=None):
        super(AsynchronousIndicator, self).\
            __init__(value, enums.Tags.ASYNCHRONOUS_INDICATOR)


# 6.8
class AsynchronousCorrelationValue(ByteString):
    def __init__(self, value=None):
        super(AsynchronousCorrelationValue, self).\
            __init__(value, enums.Tags.ASYNCHRONOUS_CORRELATION_VALUE)


# 6.9
class ResultStatus(Enumeration):

    def __init__(self, value=None):
        super(ResultStatus, self).__init__(
            enums.ResultStatus, value, enums.Tags.RESULT_STATUS)


# 6.10
class ResultReason(Enumeration):

    def __init__(self, value=None):
        super(ResultReason, self).__init__(
            enums.ResultReason, value, enums.Tags.RESULT_REASON)


# 6.11
class ResultMessage(TextString):
    def __init__(self, value=None):
        super(ResultMessage, self).__init__(value, enums.Tags.RESULT_MESSAGE)


# 6.12
class BatchOrderOption(Boolean):
    def __init__(self, value=None):
        super(BatchOrderOption, self).\
            __init__(value, enums.Tags.BATCH_ORDER_OPTION)


# 6.13
class BatchErrorContinuationOption(Enumeration):

    def __init__(self, value=None):
        super(BatchErrorContinuationOption, self).__init__(
            enums.BatchErrorContinuationOption, value,
            enums.Tags.BATCH_ERROR_CONTINUATION_OPTION)


# 6.14
class BatchCount(Integer):
    def __init__(self, value=None):
        super(BatchCount, self).__init__(value, enums.Tags.BATCH_COUNT)


# 6.16
class MessageExtension(Struct):
    def __init__(self):
        super(MessageExtension, self).__init__(enums.Tags.MESSAGE_EXTENSION)


# 9.1.3.2.2
class KeyCompressionType(Enumeration):

    def __init__(self, value=None):
        super(KeyCompressionType, self).__init__(
            enums.KeyCompressionType, value, enums.Tags.KEY_COMPRESSION_TYPE)