func setB3SpanIdHeader(request *http.Request, logger lager.Logger) { randBytes, err := secure.RandomBytes(8) if err != nil { logger.Info("failed-to-create-b3-span-id", lager.Data{"error": err.Error()}) return } id := hex.EncodeToString(randBytes) request.Header.Set(B3SpanIdHeader, id) }
func SetB3Headers(request *http.Request, logger lager.Logger) { existingTraceId := request.Header.Get(B3TraceIdHeader) existingSpanId := request.Header.Get(B3SpanIdHeader) if existingTraceId != "" && existingSpanId != "" { setB3SpanIdHeader(request, logger) setB3ParentSpanIdHeader(request, existingSpanId) if logger != nil { logger.Debug("b3-trace-id-header-exists", lager.Data{B3TraceIdHeader: existingTraceId}) } return } randBytes, err := secure.RandomBytes(8) if err != nil { logger.Info("failed-to-create-b3-trace-id", lager.Data{"error": err.Error()}) return } id := hex.EncodeToString(randBytes) request.Header.Set(B3TraceIdHeader, id) request.Header.Set(B3SpanIdHeader, request.Header.Get(B3TraceIdHeader)) }
otherAesGcm, err := secure.NewAesGCM(otherKey) Expect(err).ToNot(HaveOccurred()) decryptedText, err := otherAesGcm.Decrypt(cipherText, nonce) Expect(err).To(HaveOccurred()) Expect(err.Error()).Should(ContainSubstring("authentication failed")) Expect(decryptedText).ToNot(Equal(plainText)) }) }) Context("when using an invalid nonce", func() { It("returns an error", func() { otherNonce := []byte("0123456789AB") decryptedText, err := aesGcm.Decrypt(cipherText, otherNonce) Expect(err).To(HaveOccurred()) Expect(err.Error()).Should(ContainSubstring("authentication failed")) Expect(decryptedText).ToNot(Equal(plainText)) }) }) }) Describe("RandomBytes", func() { It("Generates a random byte array with the specified length", func() { randBytes, err := secure.RandomBytes(123) Expect(err).ToNot(HaveOccurred()) Expect(randBytes).To(HaveLen(123)) }) }) })