Class: Cybertrain::SentryDsn

Inherits:
Object
  • Object
show all
Defined in:
cybertrain/sentry_dsn.rb

Overview

The DSN that turns the Sentry sender on (CYBERTRAIN_SENTRY_DSN, Config#sentry_dsn; spec section 13.4), read by a byte scan (no regexp). The accepted form is

http[s]://[:]@[:][/]/

The secret (Sentry’s legacy second half) is accepted and ignored. Every message SentryDsn.problem gives names the part that is wrong and never repeats the key.

Examples:

dsn = Cybertrain::SentryDsn.from_text("https://abc@o1.ingest.sentry.io/42")
dsn.dsn_endpoint # => "https://o1.ingest.sentry.io/api/42/envelope/"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#dsn_endpoint ⇒ String (readonly)

Where events are POSTed: <scheme>://<host>[:<port>][/<path>]/api/<project id>/envelope/.

Returns:

  • (String)


32
33
34
# File 'cybertrain/sentry_dsn.rb', line 32

def dsn_endpoint
  @dsn_endpoint
end

#dsn_project_id ⇒ String (readonly)

The project id, digits.

Returns:

  • (String)


26
27
28
# File 'cybertrain/sentry_dsn.rb', line 26

def dsn_project_id
  @dsn_project_id
end

#dsn_public_key ⇒ String (readonly)

The public key, sent as sentry_key in X-Sentry-Auth.

Returns:

  • (String)


21
22
23
# File 'cybertrain/sentry_dsn.rb', line 21

def dsn_public_key
  @dsn_public_key
end

#dsn_public_text ⇒ String (readonly)

The DSN without its secret: <scheme>://<public key>@<host>[:<port>][/<path>]/<project id>, what an envelope’s header names (the secret never leaves the process).

Returns:

  • (String)


39
40
41
# File 'cybertrain/sentry_dsn.rb', line 39

def dsn_public_text
  @dsn_public_text
end

Class Method Details

.from_text(text) ⇒ SentryDsn

The parsed DSN; raises ArgumentError with problem’s reason when the text is not one.

Parameters:

  • text (String)

Returns:

Raises:

  • (ArgumentError)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'cybertrain/sentry_dsn.rb', line 59

def self.from_text(text)
  s = text.to_s # NOTES rule 62
  why = SentryDsn.problem(s)
  raise ArgumentError, why unless why.empty?

  start = s.start_with?("https://") ? 8 : 7
  n = s.bytesize
  slash = SentryDsn.byte_index(s, 47, start, n)
  at = SentryDsn.byte_index(s, 64, start, slash)
  key_end = SentryDsn.byte_index(s, 58, start, at)
  last_slash = SentryDsn.last_byte_index(s, 47, slash, n)
  # Fresh Strings bound to locals before .new (NOTES rule 58).
  key = s.byteslice(start, key_end - start).to_s
  project = s.byteslice(last_slash + 1, n - last_slash - 1).to_s
  scheme = s.byteslice(0, start).to_s
  host_and_port = s.byteslice(at + 1, slash - at - 1).to_s
  path = s.byteslice(slash, last_slash - slash).to_s
  endpoint = "#{scheme}#{host_and_port}#{path}/api/#{project}/envelope/"
  public_text = "#{scheme}#{key}@#{host_and_port}#{path}/#{project}"
  SentryDsn.new(key, project, endpoint, public_text)
end

.problem(text) ⇒ String

”” when text is a DSN from_text accepts, else the reason, which never contains the key: “the DSN is empty”, “a space, a control character or a non-ASCII byte is not allowed”, “the scheme must be https or http”, “a query or a fragment is not allowed”, “no public key before the @”, “the public key may hold only letters, digits, - and _”, “no host”, “the host may hold only letters, digits, . and - (or an IPv6 address in brackets)”, “the port must be a number from 1 to 65535”, “no project id”, “the project id must be digits” or “the path may hold only letters, digits, -, ., _, ~ and /”.

Parameters:

  • text (String)

Returns:

  • (String)


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
# File 'cybertrain/sentry_dsn.rb', line 93

def self.problem(text)
  s = text.to_s # NOTES rule 62
  n = s.bytesize
  return "the DSN is empty" if n == 0

  i = 0
  while i < n
    b = s.getbyte(i)
    return "a space, a control character or a non-ASCII byte is not allowed" if b <= 32 || b >= 127

    i += 1
  end
  start = 0
  if s.start_with?("https://")
    start = 8
  elsif s.start_with?("http://")
    start = 7
  end
  return "the scheme must be https or http" if start == 0
  if SentryDsn.byte_index(s, 63, start, n) < n || SentryDsn.byte_index(s, 35, start, n) < n
    return "a query or a fragment is not allowed"
  end

  # The authority ends at the first "/"; the key ends at its first "@".
  slash = SentryDsn.byte_index(s, 47, start, n)
  at = SentryDsn.byte_index(s, 64, start, slash)
  return "no public key before the @" if at == slash

  key_end = SentryDsn.byte_index(s, 58, start, at)
  return "no public key before the @" if key_end == start

  i = start
  while i < key_end
    unless SentryDsn.key_byte?(s.getbyte(i))
      return "the public key may hold only letters, digits, - and _"
    end

    i += 1
  end
  why = SentryDsn.host_problem(s, at + 1, slash)
  return why unless why.empty?

  return "no project id" if slash == n

  last_slash = SentryDsn.last_byte_index(s, 47, slash, n)
  return "no project id" if last_slash == n - 1

  i = last_slash + 1
  while i < n
    b = s.getbyte(i)
    return "the project id must be digits" if b < 48 || b > 57

    i += 1
  end
  i = slash
  while i < last_slash
    unless SentryDsn.path_byte?(s.getbyte(i))
      return "the path may hold only letters, digits, -, ., _, ~ and /"
    end

    i += 1
  end
  ""
end