1
#pragma once
2

            
3
#include "envoy/common/pure.h"
4
#include "envoy/http/message.h"
5
#include "envoy/upstream/cluster_manager.h"
6

            
7
namespace Envoy {
8
namespace Extensions {
9
namespace Common {
10
namespace Aws {
11

            
12
class MetadataFetcher;
13
using MetadataFetcherPtr = std::unique_ptr<MetadataFetcher>;
14

            
15
/**
16
 * MetadataFetcher interface can be used to retrieve AWS Metadata from various providers.
17
 * An instance of this interface is designed to retrieve one AWS Metadata at a time.
18
 * The implementation of AWS Metadata Fetcher is similar to JwksFetcher.
19
 */
20

            
21
class MetadataFetcher {
22
public:
23
  class MetadataReceiver {
24
  public:
25
    enum class Failure {
26
      /* A network error occurred causing AWS Metadata retrieval failure. */
27
      Network,
28
      /* A failure occurred when trying to parse the retrieved AWS Metadata data. */
29
      InvalidMetadata,
30
      /* A missing config causing AWS Metadata retrieval failure. */
31
      MissingConfig,
32
    };
33

            
34
    // Metadata fetcher begins in "FirstRefresh" and stays there until first success, then reverts
35
    // to standard cache duration timing. "FirstRefresh" state will cause credential refresh at 2
36
    // sec, doubling to a maximum of 30 sec until successful.
37
    enum class RefreshState {
38
      FirstRefresh,
39
      Ready,
40
    };
41

            
42
209
    virtual ~MetadataReceiver() = default;
43

            
44
    /**
45
     * @brief Successful retrieval callback of returned AWS Metadata.
46
     * @param body Fetched AWS Metadata.
47
     */
48
    virtual void onMetadataSuccess(const std::string&& body) PURE;
49

            
50
    /**
51
     * @brief Retrieval error callback.
52
     * @param reason the failure reason.
53
     */
54
    virtual void onMetadataError(Failure reason) PURE;
55
  };
56

            
57
160
  virtual ~MetadataFetcher() = default;
58

            
59
  /**
60
   * @brief Cancel any in-flight request.
61
   */
62
  virtual void cancel() PURE;
63

            
64
  /**
65
   * @brief Retrieve a AWS Metadata from a remote HTTP host.
66
   * At most one outstanding request may be in-flight.
67
   * i.e. from the invocation of `fetch()` until either
68
   * a callback or `cancel()` is invoked, no additional
69
   * `fetch()` may be issued. The URI to fetch is to pre
70
   * determined based on the credentials provider source.
71
   *
72
   * @param receiver the receiver of the fetched AWS Metadata or error
73
   */
74
  virtual void fetch(Http::RequestMessage& message, Tracing::Span& parent_span,
75
                     MetadataReceiver& receiver) PURE;
76

            
77
  /**
78
   * @brief Return MetadataReceiver Failure enum as a string.
79
   *
80
   * @return absl::string_view
81
   */
82
  virtual absl::string_view failureToString(MetadataReceiver::Failure) PURE;
83

            
84
  /**
85
   * @brief Factory method for creating a Metadata Fetcher.
86
   *
87
   * @param cm the cluster manager to use during AWS Metadata retrieval
88
   * @param provider the AWS Metadata provider
89
   * @return a MetadataFetcher instance
90
   */
91
  static MetadataFetcherPtr create(Upstream::ClusterManager& cm, absl::string_view cluster_name);
92
};
93
} // namespace Aws
94
} // namespace Common
95
} // namespace Extensions
96
} // namespace Envoy