Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
namespace Aws\CloudTrail;
3
 
4
use Aws\S3\S3Client;
5
 
6
/**
7
 * The `Aws\CloudTrail\LogRecordIterator` provides an easy way to iterate over
8
 * log records from log files generated by AWS CloudTrail.
9
 *
10
 * CloudTrail log files contain data about your AWS API calls and are stored in
11
 * Amazon S3 at a predictable path based on a bucket name, a key prefix, an
12
 * account ID, a region, and date information. The files are gzipped and
13
 * contain structured data in JSON format. This class allows you to specify
14
 * options via its factory methods, including a date range, and emits each log
15
 * record from any log files that match the provided options.
16
 *
17
 * A log record containing data about an AWS API call is yielded for each
18
 * iteration on this object.
19
 */
20
class LogRecordIterator implements \OuterIterator
21
{
22
    /** @var LogFileReader */
23
    private $logFileReader;
24
 
25
    /** @var \Iterator */
26
    private $logFileIterator;
27
 
28
    /** @var array */
29
    private $records;
30
 
31
    /** @var int */
32
    private $recordIndex;
33
 
34
    /**
35
     * @param S3Client         $s3Client
36
     * @param CloudTrailClient $cloudTrailClient
37
     * @param array            $options
38
     *
39
     * @return LogRecordIterator
40
     */
41
    public static function forTrail(
42
        S3Client $s3Client,
43
        CloudTrailClient $cloudTrailClient,
44
        array $options = []
45
    ) {
46
        $logFileIterator = LogFileIterator::forTrail(
47
            $s3Client,
48
            $cloudTrailClient,
49
            $options
50
        );
51
 
52
        return new self(new LogFileReader($s3Client), $logFileIterator);
53
    }
54
 
55
    /**
56
     * @param S3Client $s3Client
57
     * @param string   $s3BucketName
58
     * @param array    $options
59
     *
60
     * @return LogRecordIterator
61
     */
62
    public static function forBucket(
63
        S3Client $s3Client,
64
        $s3BucketName,
65
        array $options = []
66
    ) {
67
        $logFileReader = new LogFileReader($s3Client);
68
        $iter = new LogFileIterator($s3Client, $s3BucketName, $options);
69
 
70
        return new self($logFileReader, $iter);
71
    }
72
 
73
    /**
74
     * @param S3Client $s3Client
75
     * @param string   $s3BucketName
76
     * @param string   $s3ObjectKey
77
     *
78
     * @return LogRecordIterator
79
     */
80
    public static function forFile(
81
        S3Client $s3Client,
82
        $s3BucketName,
83
        $s3ObjectKey
84
    ) {
85
        $logFileReader = new LogFileReader($s3Client);
86
        $logFileIterator = new \ArrayIterator([[
87
            'Bucket' => $s3BucketName,
88
            'Key'    => $s3ObjectKey,
89
        ]]);
90
 
91
        return new self($logFileReader, $logFileIterator);
92
    }
93
 
94
    /**
95
     * @param LogFileReader $logFileReader
96
     * @param \Iterator     $logFileIterator
97
     */
98
    public function __construct(
99
        LogFileReader $logFileReader,
100
        \Iterator $logFileIterator
101
    ) {
102
        $this->logFileReader = $logFileReader;
103
        $this->logFileIterator = $logFileIterator;
104
        $this->records = array();
105
        $this->recordIndex = 0;
106
    }
107
 
108
    /**
109
     * Returns the current log record as an array.
110
     *
111
     * @return array|false
112
     */
113
    #[\ReturnTypeWillChange]
114
    public function current()
115
    {
116
        return $this->valid() ? $this->records[$this->recordIndex] : false;
117
    }
118
 
119
    #[\ReturnTypeWillChange]
120
    public function next()
121
    {
122
        $this->recordIndex++;
123
 
124
        // If all the records have been exhausted, get more records from the
125
        // next log file.
126
        while (!$this->valid()) {
127
            $this->logFileIterator->next();
128
            $success = $this->loadRecordsFromCurrentLogFile();
129
            if (!$success) {
130
                // The objects iterator is exhausted as well, so stop trying
131
                break;
132
           }
133
        }
134
    }
135
 
136
    #[\ReturnTypeWillChange]
137
    public function key()
138
    {
139
        if ($logFile = $this->logFileIterator->current()) {
140
            return $logFile['Key'] . '.' . $this->recordIndex;
141
        }
142
 
143
        return null;
144
    }
145
 
146
    #[\ReturnTypeWillChange]
147
    public function valid()
148
    {
149
        return isset($this->records[$this->recordIndex]);
150
    }
151
 
152
    #[\ReturnTypeWillChange]
153
    public function rewind()
154
    {
155
        $this->logFileIterator->rewind();
156
        $this->loadRecordsFromCurrentLogFile();
157
    }
158
 
159
    #[\ReturnTypeWillChange]
160
    public function getInnerIterator()
161
    {
162
        return $this->logFileIterator;
163
    }
164
 
165
    /**
166
     * Examines the current file in the `logFileIterator` and attempts to read
167
     * it and load log records from it using the `logFileReader`. This method
168
     * expects that items pulled from the iterator will take the form:
169
     *
170
     *     [
171
     *         'Bucket' => '...',
172
     *         'Key'    => '...',
173
     *     ]
174
     *
175
     * @return bool Returns `true` if records were loaded and `false` if no
176
     *     records were found
177
     */
178
    private function loadRecordsFromCurrentLogFile()
179
    {
180
        $this->recordIndex = 0;
181
        $this->records = array();
182
 
183
        $logFile = $this->logFileIterator->current();
184
        if ($logFile && isset($logFile['Bucket']) && isset($logFile['Key'])) {
185
            $this->records = $this->logFileReader->read(
186
                $logFile['Bucket'],
187
                $logFile['Key']
188
            );
189
        }
190
 
191
        return (bool) $logFile;
192
    }
193
}