You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

257 lines
9.2 KiB

  1. // Copyright 2016 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package logadmin
  15. import (
  16. "context"
  17. "errors"
  18. "fmt"
  19. vkit "cloud.google.com/go/logging/apiv2"
  20. "google.golang.org/api/iterator"
  21. logpb "google.golang.org/genproto/googleapis/logging/v2"
  22. maskpb "google.golang.org/genproto/protobuf/field_mask"
  23. )
  24. // Sink describes a sink used to export log entries outside Stackdriver
  25. // Logging. Incoming log entries matching a filter are exported to a
  26. // destination (a Cloud Storage bucket, BigQuery dataset or Cloud Pub/Sub
  27. // topic).
  28. //
  29. // For more information, see https://cloud.google.com/logging/docs/export/using_exported_logs.
  30. // (The Sinks in this package are what the documentation refers to as "project sinks".)
  31. type Sink struct {
  32. // ID is a client-assigned sink identifier. Example:
  33. // "my-severe-errors-to-pubsub".
  34. // Sink identifiers are limited to 1000 characters
  35. // and can include only the following characters: A-Z, a-z,
  36. // 0-9, and the special characters "_-.".
  37. ID string
  38. // Destination is the export destination. See
  39. // https://cloud.google.com/logging/docs/api/tasks/exporting-logs.
  40. // Examples: "storage.googleapis.com/a-bucket",
  41. // "bigquery.googleapis.com/projects/a-project-id/datasets/a-dataset".
  42. Destination string
  43. // Filter optionally specifies an advanced logs filter (see
  44. // https://cloud.google.com/logging/docs/view/advanced_filters) that
  45. // defines the log entries to be exported. Example: "logName:syslog AND
  46. // severity>=ERROR". If omitted, all entries are returned.
  47. Filter string
  48. // WriterIdentity must be a service account name. When exporting logs, Logging
  49. // adopts this identity for authorization. The export destination's owner must
  50. // give this service account permission to write to the export destination.
  51. WriterIdentity string
  52. // IncludeChildren, when set to true, allows the sink to export log entries from
  53. // the organization or folder, plus (recursively) from any contained folders, billing
  54. // accounts, or projects. IncludeChildren is false by default. You can use the sink's
  55. // filter to choose log entries from specific projects, specific resource types, or
  56. // specific named logs.
  57. //
  58. // Caution: If you enable this feature, your aggregated export sink might export
  59. // a very large number of log entries. To avoid exporting too many log entries,
  60. // design your aggregated export sink filter carefully, as described on
  61. // https://cloud.google.com/logging/docs/export/aggregated_exports.
  62. IncludeChildren bool
  63. }
  64. // CreateSink creates a Sink. It returns an error if the Sink already exists.
  65. // Requires AdminScope.
  66. func (c *Client) CreateSink(ctx context.Context, sink *Sink) (*Sink, error) {
  67. return c.CreateSinkOpt(ctx, sink, SinkOptions{})
  68. }
  69. // CreateSinkOpt creates a Sink using the provided options. It returns an
  70. // error if the Sink already exists. Requires AdminScope.
  71. func (c *Client) CreateSinkOpt(ctx context.Context, sink *Sink, opts SinkOptions) (*Sink, error) {
  72. ls, err := c.sClient.CreateSink(ctx, &logpb.CreateSinkRequest{
  73. Parent: c.parent,
  74. Sink: toLogSink(sink),
  75. UniqueWriterIdentity: opts.UniqueWriterIdentity,
  76. })
  77. if err != nil {
  78. return nil, err
  79. }
  80. return fromLogSink(ls), nil
  81. }
  82. // SinkOptions define options to be used when creating or updating a sink.
  83. type SinkOptions struct {
  84. // Determines the kind of IAM identity returned as WriterIdentity in the new
  85. // sink. If this value is omitted or set to false, and if the sink's parent is a
  86. // project, then the value returned as WriterIdentity is the same group or
  87. // service account used by Stackdriver Logging before the addition of writer
  88. // identities to the API. The sink's destination must be in the same project as
  89. // the sink itself.
  90. //
  91. // If this field is set to true, or if the sink is owned by a non-project
  92. // resource such as an organization, then the value of WriterIdentity will
  93. // be a unique service account used only for exports from the new sink.
  94. UniqueWriterIdentity bool
  95. // These fields apply only to UpdateSinkOpt calls. The corresponding sink field
  96. // is updated if and only if the Update field is true.
  97. UpdateDestination bool
  98. UpdateFilter bool
  99. UpdateIncludeChildren bool
  100. }
  101. // DeleteSink deletes a sink. The provided sinkID is the sink's identifier, such as
  102. // "my-severe-errors-to-pubsub".
  103. // Requires AdminScope.
  104. func (c *Client) DeleteSink(ctx context.Context, sinkID string) error {
  105. return c.sClient.DeleteSink(ctx, &logpb.DeleteSinkRequest{
  106. SinkName: c.sinkPath(sinkID),
  107. })
  108. }
  109. // Sink gets a sink. The provided sinkID is the sink's identifier, such as
  110. // "my-severe-errors-to-pubsub".
  111. // Requires ReadScope or AdminScope.
  112. func (c *Client) Sink(ctx context.Context, sinkID string) (*Sink, error) {
  113. ls, err := c.sClient.GetSink(ctx, &logpb.GetSinkRequest{
  114. SinkName: c.sinkPath(sinkID),
  115. })
  116. if err != nil {
  117. return nil, err
  118. }
  119. return fromLogSink(ls), nil
  120. }
  121. // UpdateSink updates an existing Sink. Requires AdminScope.
  122. //
  123. // WARNING: UpdateSink will always update the Destination, Filter and IncludeChildren
  124. // fields of the sink, even if they have their zero values. Use UpdateSinkOpt
  125. // for more control over which fields to update.
  126. func (c *Client) UpdateSink(ctx context.Context, sink *Sink) (*Sink, error) {
  127. return c.UpdateSinkOpt(ctx, sink, SinkOptions{
  128. UpdateDestination: true,
  129. UpdateFilter: true,
  130. UpdateIncludeChildren: true,
  131. })
  132. }
  133. // UpdateSinkOpt updates an existing Sink, using the provided options. Requires AdminScope.
  134. //
  135. // To change a sink's writer identity to a service account unique to the sink, set
  136. // opts.UniqueWriterIdentity to true. It is not possible to change a sink's writer identity
  137. // from a unique service account to a non-unique writer identity.
  138. func (c *Client) UpdateSinkOpt(ctx context.Context, sink *Sink, opts SinkOptions) (*Sink, error) {
  139. mask := &maskpb.FieldMask{}
  140. if opts.UpdateDestination {
  141. mask.Paths = append(mask.Paths, "destination")
  142. }
  143. if opts.UpdateFilter {
  144. mask.Paths = append(mask.Paths, "filter")
  145. }
  146. if opts.UpdateIncludeChildren {
  147. mask.Paths = append(mask.Paths, "include_children")
  148. }
  149. if opts.UniqueWriterIdentity && len(mask.Paths) == 0 {
  150. // Hack: specify a deprecated, unchangeable field so that we have a non-empty
  151. // field mask. (An empty field mask would cause the destination, filter and include_children
  152. // fields to be changed.)
  153. mask.Paths = append(mask.Paths, "output_version_format")
  154. }
  155. if len(mask.Paths) == 0 {
  156. return nil, errors.New("logadmin: UpdateSinkOpt: nothing to update")
  157. }
  158. ls, err := c.sClient.UpdateSink(ctx, &logpb.UpdateSinkRequest{
  159. SinkName: c.sinkPath(sink.ID),
  160. Sink: toLogSink(sink),
  161. UniqueWriterIdentity: opts.UniqueWriterIdentity,
  162. UpdateMask: mask,
  163. })
  164. if err != nil {
  165. return nil, err
  166. }
  167. return fromLogSink(ls), err
  168. }
  169. func (c *Client) sinkPath(sinkID string) string {
  170. return fmt.Sprintf("%s/sinks/%s", c.parent, sinkID)
  171. }
  172. // Sinks returns a SinkIterator for iterating over all Sinks in the Client's project.
  173. // Requires ReadScope or AdminScope.
  174. func (c *Client) Sinks(ctx context.Context) *SinkIterator {
  175. it := &SinkIterator{
  176. it: c.sClient.ListSinks(ctx, &logpb.ListSinksRequest{Parent: c.parent}),
  177. }
  178. it.pageInfo, it.nextFunc = iterator.NewPageInfo(
  179. it.fetch,
  180. func() int { return len(it.items) },
  181. func() interface{} { b := it.items; it.items = nil; return b })
  182. return it
  183. }
  184. // A SinkIterator iterates over Sinks.
  185. type SinkIterator struct {
  186. it *vkit.LogSinkIterator
  187. pageInfo *iterator.PageInfo
  188. nextFunc func() error
  189. items []*Sink
  190. }
  191. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  192. func (it *SinkIterator) PageInfo() *iterator.PageInfo { return it.pageInfo }
  193. // Next returns the next result. Its second return value is Done if there are
  194. // no more results. Once Next returns Done, all subsequent calls will return
  195. // Done.
  196. func (it *SinkIterator) Next() (*Sink, error) {
  197. if err := it.nextFunc(); err != nil {
  198. return nil, err
  199. }
  200. item := it.items[0]
  201. it.items = it.items[1:]
  202. return item, nil
  203. }
  204. func (it *SinkIterator) fetch(pageSize int, pageToken string) (string, error) {
  205. return iterFetch(pageSize, pageToken, it.it.PageInfo(), func() error {
  206. item, err := it.it.Next()
  207. if err != nil {
  208. return err
  209. }
  210. it.items = append(it.items, fromLogSink(item))
  211. return nil
  212. })
  213. }
  214. func toLogSink(s *Sink) *logpb.LogSink {
  215. return &logpb.LogSink{
  216. Name: s.ID,
  217. Destination: s.Destination,
  218. Filter: s.Filter,
  219. IncludeChildren: s.IncludeChildren,
  220. OutputVersionFormat: logpb.LogSink_V2,
  221. // omit WriterIdentity because it is output-only.
  222. }
  223. }
  224. func fromLogSink(ls *logpb.LogSink) *Sink {
  225. return &Sink{
  226. ID: ls.Name,
  227. Destination: ls.Destination,
  228. Filter: ls.Filter,
  229. WriterIdentity: ls.WriterIdentity,
  230. IncludeChildren: ls.IncludeChildren,
  231. }
  232. }