<commit_before>package com.lookout.borderpatrol.session

import com.twitter.util.{Throw, Time}
import scala.concurrent.duration.Duration
import scala.concurrent.{Future => ScalaFuture, Promise => ScalaPromise, Await}
import scala.concurrent.duration._
import scala.util.{Failure, Success, Try}
import com.lookout.borderpatrol.session._
import concurrent.ExecutionContext.Implicits.global

/**
 * This prototypes out an API for the SecretStore, keeping secrets in memory
 * which obviously doesn't work in a multi-server environment.
 *
 * Further work should be done to coordinate secrets among processes.
 *
 * For example, a zookeeper watcher could update current and previous in memory
 * on change, while an external service handles writing new secrets.
 */
sealed trait SecretStoreApi {
  def current: Secret
  def previous: Secret
  def find(f: (Secret) => Boolean): Try[Secret]
}

case class ConsulSecretStore(watcher: SecretsWatcherApi) extends SecretStoreApi {
  //During initialization, we want this to be a hard failure that prevents server from starting
  //private[this] var _secrets: Secrets = Await.result[Secrets]( watcher.getSecrets,Duration(2000, MILLISECONDS))

  //If we unable to get secrets. Hard fail
  private[this] var _secrets: Secrets = watcher.initialSecrets match {
    case Success(secrets) => secrets
    case Failure(f) => {
      throw new IllegalArgumentException(f)
    }
  }

  /**
   * Get Next secrets
   * @return
   */
  private def nextSecrets: Secrets = {
    watcher.getNext match {
      case Success(newSecrets) => newSecrets
      case Failure(f) => {
        //Do something indicating we got an error
        println(s"Unable to get new secrets, logging.........................>$f")
        _secrets
      }
    }
  }

  def current = {
    val c = _secrets.current
    if (c.expiry > Time.now && c.expiry <= SecretExpiry.currentExpiry) c
    else {
      println("Secrets have expired..................................................................................>")
      _secrets = nextSecrets
    }
    _secrets.current
  }

  def previous = _secrets.previous

  def find(f: (Secret) => Boolean) =
    if (f(current)) Success(current)
    else if (f(previous)) Success(previous)
    else Failure(new Exception("No matching secrets found"))
}

case class InMemorySecretStore(secrets: Secrets) extends SecretStoreApi {
  import com.lookout.borderpatrol.session.SecretExpiry._<commit_msg>Code cleanup<commit_after>package com.lookout.borderpatrol.session

import com.twitter.util.{Throw, Time}
import scala.concurrent.duration.Duration
import scala.concurrent.{Future => ScalaFuture, Promise => ScalaPromise, Await}
import scala.concurrent.duration._
import scala.util.{Failure, Success, Try}
import com.lookout.borderpatrol.session._
import concurrent.ExecutionContext.Implicits.global

/**
 * This prototypes out an API for the SecretStore, keeping secrets in memory
 * which obviously doesn't work in a multi-server environment.
 *
 * Further work should be done to coordinate secrets among processes.
 *
 * For example, a zookeeper watcher could update current and previous in memory
 * on change, while an external service handles writing new secrets.
 */
sealed trait SecretStoreApi {
  def current: Secret
  def previous: Secret
  def find(f: (Secret) => Boolean): Try[Secret]
}

case class ConsulSecretStore(watcher: SecretsWatcherApi) extends SecretStoreApi {
  //During initialization, we want this to be a hard failure that prevents server from starting
  private[this] var _secrets: Secrets = watcher.initialSecrets match {
    case Success(secrets) => secrets
    case Failure(f) => {
      throw new IllegalArgumentException(f)
    }
  }

  /**
   * Get Next secrets
   * @return
   */
  private def nextSecrets: Secrets = {
    watcher.getNext match {
      case Success(newSecrets) => newSecrets
      case Failure(f) => {
        //Do something indicating we got an error
        println(s"Unable to get new secrets: Exception $f")
        _secrets
      }
    }
  }

  def current = {
    val c = _secrets.current
    if (c.expiry > Time.now && c.expiry <= SecretExpiry.currentExpiry) c
    else {
      println("Secrets have expired")
      _secrets = nextSecrets
    }
    _secrets.current
  }

  def previous = _secrets.previous

  def find(f: (Secret) => Boolean) =
    if (f(current)) Success(current)
    else if (f(previous)) Success(previous)
    else Failure(new Exception("No matching secrets found"))
}

case class InMemorySecretStore(secrets: Secrets) extends SecretStoreApi {
  import com.lookout.borderpatrol.session.SecretExpiry._
{'fraction_non_alphanumeric': 0.11267605633802817, 'fraction_numerical': 0.000866738894907909, 'mean_word_length': 3.3465160075329567, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '143530', 'n_tokens_mistral': 1347, 'n_tokens_neox': 1295, 'n_words': 414}